Sorting date using Comparator.

Hi frnds,

I have a problem sorting a date using comparator Interface. To the compare method I am passing Objects, which contains values retrieved from bean.

I have hyperlinks for all the fields, upon clicking am able to sort all the fields except the date:

The format of date is MM/DD/YYYY. It is sorting only taking month into consideration. I want the date to be sorted completely taking into consideration the complete date.

Kindly help me in this regards.

Below is the code listed:

publicint compare(Object vendoremployee1, Object vendoremployee2){

String strSowTitle1 = ((CVendorEmployees) vendoremployee1).getSowTitle().toUpperCase();

String strSowNum1 = ((CVendorEmployees) vendoremployee1).getSowNumber().toUpperCase();

String lastName1 = ((CVendorEmployees) vendoremployee1).getLastName().toUpperCase();

String strCreatedDate1 = ((CVendorEmployees) vendoremployee1).getCreatedDate().toUpperCase();

String strSowTitle2 = ((CVendorEmployees) vendoremployee2).getSowTitle().toUpperCase();

String strSowNum2 = ((CVendorEmployees) vendoremployee2).getSowNumber().toUpperCase();

String lastName2 = ((CVendorEmployees) vendoremployee2).getLastName().toUpperCase();

String strCreatedDate2 = ((CVendorEmployees) vendoremployee2).getCreatedDate().toUpperCase();

// How do i sort

strCreatedDate1.compareTo(strCreatedDate2);

Thanks

[1571 byte] By [beejuma] at [2007-11-27 9:45:58]
# 1

II am assuming that you are using either the Collections.sort() method, passing in a Vector of your objects or the Arrays.sort() method passing in an array of your objects. Either way your class definition for your object must implement Comparable. Then you have to implement the compareTo(Object o) method. Inside this method you can setup the sort any way you want. If you want the object to sort by the date member, the easiest way to accomplish the sort is to get the milliseconds from EPOC. Then you can sort either ascending or descending.

Example code: forgive the formatting, cut and paste it into an editor

The example has the data member as a Long object.

Hope this is helpful.

public int compareTo( Object o ) throws ClassCastException

{

YourClassNameHere obj;

if( o instanceof YourClassNameHere)

{

obj = (YourClassNameHere) o;

}

else

{

throw new ClassCastException("Specified Object o is not of type YourClassNameHere." );

}

//Only if these are not primitives

if( this.getYourDatamember() != null && obj.getYourDatamember() != null)

{

if(this. getYourDatamember().longValue() < obj. getYourDatamember().longValue())

{

return -1;

}

else if( this. getYourDatamember().longValue() > obj.getYourDatamember().longValue() )

{

return 1;

}

else

{

return 0;

}

}

else if(this.getYourDatamember != null && obj.getYourDatamember() == null)

{

return -1;

}

else if(this.getYourDatamember == null && obj.getYourDatamember() != null)

{

return 1;

}

else

{

return 0;

}

}

Colorado_Codera at 2007-7-12 23:55:34 > top of Java-index,Core,Core APIs...
# 2
Why are you storing the bean's date component as a string? The java.util.Date class implements the Comparable interface therefore date objects can be sorted using the compareTo method. Change your bean to use date objects to store dates.
paternostroa at 2007-7-12 23:55:34 > top of Java-index,Core,Core APIs...
# 3
I dont have a single date field, I am retreiving the values from the database like ID, title,firstname and date. So I am setting these values in the bean. And while retrieving these values, I am converting all to a Stirng and passing it to a Compartor.
beejuma at 2007-7-12 23:55:34 > top of Java-index,Core,Core APIs...
# 4

That's my point. I'm sure the dates are stored as SQL dates in the database. Retrieve them as dates versus converting them to strings. If you insist on converting everything to strings when storing the data in the bean then you will need to convert those date strings back to date objects using the SimpleDateFormat class. As date objects sorting is much easier as the java.util.Date class (as mentioned in my previous post) implements the Comparable interface.

paternostroa at 2007-7-12 23:55:34 > top of Java-index,Core,Core APIs...