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

前端 未结 6 1625
名媛妹妹
名媛妹妹 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条回答
  • 2020-12-15 21:11

    Date's compareTo() you're using will work for ascending order.

    To do descending, just reverse the value of compareTo() coming out. You can use a single Comparator class that takes in 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 actually sort the list.

    As a side note, I'm not sure why you're defining your map inside your for loop. I'm not exactly sure what your code is trying to do, but I assume you want to populate the indexed values from your for loop in to the map.

    0 讨论(0)
  • 2020-12-15 21:22

    If date in string format convert it to 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());
                  }
                });
    
    0 讨论(0)
  • 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<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
        }
    };
    
    0 讨论(0)
  • 2020-12-15 21:27

    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());
    
    0 讨论(0)
  • 2020-12-15 21:28

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

    From Java Doc

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

    0 讨论(0)
  • 2020-12-15 21:31

    Easier alternative to above answers

    1. If Object(Model Class/POJO) contains the date in String datatype.

      private void sortArray(ArrayList<myObject> arraylist) {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); //your own date format
      if (reports != null) {
          Collections.sort(arraylist, new Comparator<myObject>() {
              @Override
              public int compare(myObject o1, myObject o2) {
                  try {
                      return simpleDateFormat.parse(o2.getCreated_at()).compareTo(simpleDateFormat.parse(o1.getCreated_at()));
                  } catch (ParseException e) {
                      e.printStackTrace();
                      return 0;
                  }
              }
          });
      }
      
    2. If Object(Model Class/POJO) contains date in Date datatype

      private void sortArray(ArrayList<myObject> arrayList) {
      if (arrayList != null) {
          Collections.sort(arrayList, new Comparator<myObject>() {
              @Override
              public int compare(myObject o1, myObject o2) {
                  return o2.getCreated_at().compareTo(o1.getCreated_at()); }
          });
      } }
      

    The above code is for sorting the array in descending order of date, swap o1 and o2 for ascending order.

    0 讨论(0)
提交回复
热议问题