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

前端 未结 6 1638
名媛妹妹
名媛妹妹 2020-12-15 21:04

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

6条回答
  •  -上瘾入骨i
    2020-12-15 21:24

    Just add like 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 end of the your class

    static final Comparator byDate = new Comparator() {
        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
        }
    };
    

提交回复
热议问题