Home > OS >  Sorting date in decending order [duplicate]
Sorting date in decending order [duplicate]

Time:10-07

I'm trying to sort these dates into descending order. But Collections.reverseOrder gives me an error. I tried sorting in descending order I can't find the error.

public class Mine {
    public static void main(String[] args) {
        List<String> dateList = new ArrayList<String>();
        
        dateList.add("09/07/2021");
        dateList.add("06/02/2021");
        dateList.add("02/01/2021");
        dateList.add("01/04/2021");
    
        
        
        sortDates(dateList);
        
        for (String sortedDate : dateList) {
            System.out.println(sortedDate);
        }
    }

    private static void sortDates(List<String> dateList) {
        // TODO Auto-generated method stub
        Collections.reverseOrder(dateList,  new Comparator<String>() {
            DateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
            @Override
            public int compare (String DATE_2,String DATE_1) {
                
            try {
                return dateformat.parse(DATE_2).compareTo(dateformat.parse(DATE_1));
            
                }
                catch(ParseException e){
                    throw new IllegalArgumentException(e);
                }
                
            }
            
        });
        
    }

}

CodePudding user response:

You can do it as follows:

  • Use LocalDate and a DateTimeFormatter to parse the date.
dateList.add("02/01/2021");
dateList.add("06/02/2021");
dateList.add("01/04/2021");
dateList.add("09/07/2021");

DateTimeFormatter dtf =
        DateTimeFormatter.ofPattern("MM/dd/yyyy");
Collections.sort(dateList,
        Comparator.comparing(
                date -> LocalDate.parse(date, dtf), Comparator.reverseOrder()));
                

for (String sortedDate : dateList) {
    System.out.println(sortedDate);
}

prints

09/07/2021
06/02/2021
02/01/2021
01/04/2021

For a large list of dates, It might be best to store them as LocalDate objects so that the comparator need not parse them each time the object is encountered. You could then format them as you display them.

CodePudding user response:

public static void main(String... args) {
    List<String> dates = List.of(
            "09/07/2021",
            "06/02/2021",
            "02/01/2021",
            "01/04/2021");

    List<String> sortedDates = dates.stream()
                                    .sorted(sortDesc())
                                    .collect(Collectors.toList());
}

private static Comparator<String> sortDesc() {
    Comparator<String> sortByYearAsc = Comparator.comparingInt(date -> Integer.parseInt(date.substring(6)));
    Comparator<String> sortByMonthAsc = Comparator.comparingInt(date -> Integer.parseInt(date.substring(3, 5)));
    Comparator<String> sortByDayAsc = Comparator.comparingInt(date -> Integer.parseInt(date.substring(0, 3)));
    return sortByYearAsc.thenComparing(sortByMonthAsc).thenComparing(sortByDayAsc).reversed();
}
  •  Tags:  
  • java
  • Related