Sort objects in ArrayList by date?

后端 未结 14 2129
生来不讨喜
生来不讨喜 2020-11-22 06:58

Every example I find is about doing this alphabetically, while I need my elements sorted by date.

My ArrayList contains objects on which one of the datamembers is a

相关标签:
14条回答
  • 2020-11-22 07:20

    Use the below approach to identify dates are sort or not

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    
    boolean  decendingOrder = true;
        for(int index=0;index<date.size() - 1; index++) {
            if(simpleDateFormat.parse(date.get(index)).getTime() < simpleDateFormat.parse(date.get(index+1)).getTime()) {
                decendingOrder = false;
                break;
            }
        }
        if(decendingOrder) {
            System.out.println("Date are in Decending Order");
        }else {
            System.out.println("Date not in Decending Order");
        }       
    }   
    
    0 讨论(0)
  • 2020-11-22 07:26

    Pass the ArrayList In argument.

        private static void order(ArrayList<Object> list) {
    
        Collections.sort(list, new Comparator() {
    
            public int compare(Object o2, Object o1) {
    
                String x1 =  o1.Date;
                String x2 =  o2.Date;
    
                    return  x1.compareTo(x2);
    
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题