简体   繁体   中英

how to sort date in descending order using comparator

I have a bean object testBean with getter setter and methods.I am retrieving the results from the database and storing it in a TreeMap

The Output of the Map will look like this:

{Student1 = [testBean[Dept=Science,ID=12,grade=A,Date=12-Jan-2013]]
            [testBean[Dept=Science,ID=12,grade=B,Date=14-Mar-2013]]

{Student2 = [testBean[Dept=Science,ID=02,grade=A,Date=12-Jan-2013]]
            [testBean[Dept=Science,ID=02,grade=A,Date=14-Mar-2013]]

I need the Output to be arranged in Descending order so that the latest date comes first. So I am using a comparator to sort the date:

public int DateCompare(Object studentObj, Object anotherStudentObj) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    String value = ((testBean) studentObj).getDateTmTrans();
    String value1 = ((testBean) anotherStudentObj).getDateTmTrans();
    int retVal = 0;

    try {

        Date firstDate = dateFormat.parse(value);
        Date secondDate = dateFormat.parse(value1);     
        retVal = firstDate.compareTo(secondDate);

    } catch (ParseException e) {
        e.printStackTrace();
    }       
    return 0;
}

But I couldn't able to sort the date in descending order. Is there any solution to get the desired output?

Any suggestions are welcome

Thanks in advance

But i couldn't able to sort the date in descending order.

Two easy options:

  • You could just reverse your comparison yourself, using secondDate.compareTo(firstDate) . (I assume that in your real code you're actually returning retVal ; it's ignored in your posted code.)
  • Call Collections.reverseOrder(Comparator) to create a comparator with the reverse order of the original one.

Instead of comparing firstDate against secondDate , compare secondDate against firstDate :

retVal = secondDate.compareTo(firstDate);

And don't forget to return retVal instead of 0 ;)

you could add a boolean parameter to your comparator's constructor, eg boolean descending and then do this:

retVal = (descending ? -1 : 1) * firstDate.compareTo(secondDate);

here, if you created your comparator by passing true as "descending" argument, it would flip the sign around, leave 0 unaffected, and effectively you'd end up with a comparator that is easily configurable to help you ordering dates in ascending/descending order.

You can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM