Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Sort Date in descending order From Arraylist Date in android?

I have an ArrayList which stores Dates and I sorted them in descending order. Now I want to display them in a ListView. This is what I did 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) {
            }

        });
like image 210
crickpatel0024 Avatar asked Oct 29 '12 10:10

crickpatel0024


People also ask

How do you sort a list by date?

In the main method create an ArrayList of type DateItem. Store the objects of DateItem in the ArrayList. Create another class called sortItems which implements Comparator and pass our DateItem class to a comparator.

How do you sort time in ascending order in Java?

1. Using Collections. sort() method. Collections utility class provides a static sort() method for sorting the specified list into ascending order, according to the natural ordering of its elements.


1 Answers

Create Arraylist<Date> of Date class. And use Collections.sort() for ascending order.

See sort(List<T> list)

Sorts the specified list into ascending order, according to the natural ordering of its elements.

For Sort it in descending order See Collections.reverseOrder()

Collections.sort(yourList, Collections.reverseOrder());
like image 80
Sumit Singh Avatar answered Oct 12 '22 01:10

Sumit Singh