How to sort date in descending order From Arraylist Date in android?

I have an ArrayList that stores Date , and I sorted them in descending order. Now I want to show them in a ListView . This is what I have done so far:

  spndata.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) { switch (position) { case 0: list = DBAdpter.requestUserData(assosiatetoken); for (int i = 0; i < list.size(); i++) { if (list.get(i).lastModifiedDate != null) { lv.setAdapter(new MyListAdapter( getApplicationContext(), list)); } } break; case 1: list = DBAdpter.requestUserData(assosiatetoken); Calendar c = Calendar.getInstance(); SimpleDateFormat df3 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); String formattedDate3 = df3.format(c.getTime()); Log.v("log_tag", "Date " + formattedDate3); for (int i = 0; i < list.size(); i++) { if (list.get(i).submitDate != null) { String sDate = list.get(i).submitDate; SimpleDateFormat df4 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); String formattedDate4 = df4.format(sDate); Map<Date, Integer> dateMap = new TreeMap<Date, Integer>(new Comparator<Date>(){ public int compare(Date formattedDate3, Date formattedDate4) { return formattedDate3.compareTo(formattedDate4); } }); lv.setAdapter(new MyListAdapter( getApplicationContext(), list)); } } break; case 2: break; case 3: break; default: break; } } public void onNothingSelected(AdapterView<?> arg0) { } }); 
+6
source share
5 answers

Create an Arraylist<Date> of the Date class. And use Collections.sort() to grow.

See sort (List <T> list)

Sorts the specified list in ascending order, in accordance with the natural ordering of its elements.

To sort in descending order See Collections.reverseOrder ()

 Collections.sort(yourList, Collections.reverseOrder()); 
+11
source

Just add this in case 1: like this

  case 0: list = DBAdpter.requestUserData(assosiatetoken); Collections.sort(list, byDate); for (int i = 0; i < list.size(); i++) { if (list.get(i).lastModifiedDate != null) { lv.setAdapter(new MyListAdapter( getApplicationContext(), list)); } } break; 

and put this method at the end of your class

 static final Comparator<All_Request_data_dto> byDate = new Comparator<All_Request_data_dto>() { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) { Date d1 = null; Date d2 = null; try { d1 = sdf.parse(ord1.lastModifiedDate); d2 = sdf.parse(ord2.lastModifiedDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return (d1.getTime() > d2.getTime() ? -1 : 1); //descending // return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending } }; 
+14
source

The date compareTo () that you use will work in ascending order.

To take a snapshot, simply change the compareTo () value. You can use one Comparator class that accepts a flag / enum in the constructor that identifies the sort order

 public int compare(MyObject lhs, MyObject rhs) { if(SortDirection.Ascending == m_sortDirection) { return lhs.MyDateTime.compareTo(rhs.MyDateTime); } return rhs.MyDateTime.compareTo(lhs.MyDateTime); } 

You need to call Collections.sort () to sort the list.

As a side note, I'm not sure why you define your map inside a for loop. I'm not quite sure what your code is trying to do, but I assume that you want to populate the indexed values ​​from the for loop on the map.

+5
source

If a date in string format converts it to a date format for each object:

 String argmodifiledDate = "2014-04-06 22:26:15"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { this.modifiledDate = format.parse(argmodifiledDate); } catch (ParseException e) { e.printStackTrace(); } 

Then sort the arraylist in descending order:

 ArrayList<Document> lstDocument= this.objDocument.getArlstDocuments(); Collections.sort(lstDocument, new Comparator<Document>() { public int compare(Document o1, Document o2) { if (o1.getModifiledDate() == null || o2.getModifiledDate() == null) return 0; return o2.getModifiledDate().compareTo(o1.getModifiledDate()); } }); 
+4
source

Date Comparable , so just create a List<Date> and sort it using Collections.sort() . And use Collections.reverseOrder() to get the comparator in reverse ordering .

From Java Doc

Returns a comparator that reverses the specified comparator. If the specified comparator is NULL, this method is equivalent to reverseOrder () (in other words, it returns a comparator that superimposes the reverse side of natural ordering on a collection of objects that implement the Comparable interface).

+2
source

All Articles