Other ways of sorting List<Date> in Codename One

Diamond :

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));

Morteza Jalambadani :

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());
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=90129&siteId=1