This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Date Ascending order Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date Ascending order Comparator" Watch "Date Ascending order Comparator" New topic
Author

Date Ascending order Comparator

Adrian Enns
Ranch Hand

Joined: Aug 11, 2004
Posts: 48
I have a comparator that sorts a collection of objects by date in descending order, but I'm having problem creating a comparator that sorts by date in ascending order. Can you help?

The code for the desceding comparator is:

private class DateComparatorDescending implements Comparator
{
public int compare(Object one, Object two)
{
CmnProbEduTraining objOne = (CmnProbEduTraining)one;
CmnProbEduTraining objTwo = (CmnProbEduTraining)two;

if (objOne.getInsertedDt().after(objTwo.getInsertedDt()))
{
return -1;
}
else if (objOne.getInsertedDt().before(objTwo.getInsertedDt()))
{
return +1;
}
else
{
return 0;
}
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

And what happens when you substitute negative one for one, and visa versa?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Adrian Enns
Ranch Hand

Joined: Aug 11, 2004
Posts: 48
Thanks, that worked. I had tried that, but my changed code wasn't getting deployed to my server for some reason. Thanks.
 
jQuery in Action, 2nd edition
 
subject: Date Ascending order Comparator