Sort the list's date elements in Android

Source: Internet
Author: User

Recently, in your project, you need to sort the read data in descending order of time.

The specific steps are as follows:

1. Read the data and deposit it in the list

2. Take the timestamp from the data and convert it from string to date

3. Sort elements in list by date using bubbling sort

The specific code is as follows:

Sort the list in reverse chronological order

@SuppressLint ("SimpleDateFormat")

Private list<testentity> invertorderlist (list<testentity> L) {

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

Date D1;

Date D2;

Testentity temp_r = new Testentity ();

Do a bubble sort, big in the top of the array

for (int i=0; I<l.size ()-1; i++) {

for (int j=i+1; j<l.size (); j + +) {

Parseposition pos1 = new Parseposition (0);

Parseposition Pos2 = new Parseposition (0);

D1 = Sdf.parse (L.get (i). GetDate (), POS1);

D2 = Sdf.parse (L.get (j). GetDate (), POS2);

if (D1.before (D2)) {//If the team pre-date, swap order

Temp_r = L.get (i);

L.set (I, L.get (j));

L.set (J, Temp_r);

}

}

}

return L;

}

Sort the list's date elements in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.