aspose file tools*
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 Spring in Action this week in the Spring 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: 18896
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Ascending order Comparator