Other ways of sorting List<Date> in Codename One

Viewed 164

Due to Codename One not implementing the full Java API, is there another working way of sorting List of Dates?

I've tried below, both of which failed to compile.

datesList.sort(Date::compareTo); // Failed

error: invalid method reference datesList.sort(Date::compareTo);

// Failed
Collections.sort(datesList, new Comparator<Date>(){
    public int compare (Date d1, Date d2){
        return d1.compareTo(d2);
    }
});

error: cannot find symbol return d1.compareTo(d2);

The same issue with getting the last day of the month with below:

cal.getActualMaximum(Calendar.DATE);

error: cannot find symbol cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));

1 Answers

I suggest another soultion for sortig.You can use the connection sorting like:

dateList = 
    datesList
    .stream()
    .sorted( 
        Comparator.comparingLong( e -> e.getTime() ) 
    ).collect( 
        Collectors.toList() 
    );

if you has not stream change the comparing method to:

Collections.sort(datesList, new Comparator<Date>(){
    public int compare (Date d1, Date d2){
        return Long.compare( d1.getTime() , d2.getTime());
    }
});
Related