Sort dates 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:

The List is sorted in reverse chronological order @suppresslint ("SimpleDateFormat") Private list<testentity> invertorderlist (list< Testentity> L) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");D ate d1;date D2; Testentity temp_r = new testentity ();//Make a bubble sort, large in the array of the top 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 front of the team before date, the order of exchange   Temp_r = L.get (i);   L.set (I, L.get (j));   L.set (J, Temp_r);}}   return L;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sort dates in Android

Related Article

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.