Trying to understand the following conclusion:
public class CommunicationComparator implements Comparator<Communication> { @Override public int compare(Communication comm1, Communication comm2) { long t1 = comm1.getDate().getTime(); long t2 = comm2.getDate().getTime(); return (int) (t2 - t1); } }
The getDate () method returns java.sql.Timestamp.
Here is the result before sorting:
for (Communication n : retVal) { System.out.println(n.getDate().toString()); }
2012-10-03 10: 02: 02.0
2012-10-07 03: 02: 01.0
2012-10-08 13: 02: 02.0
2012-10-09 03: 02: 00.0
2012-11-26 10: 02: 05.0
2012-11-28 11: 28: 11.0
2012-12-03 12: 03: 01.0
2012-12-06 15: 03: 01.0
2012-12-13 14: 03: 00.0
2012-12-28 11: 03: 00.0
2012-12-28 13: 49: 21.0
And after:
Collections.sort(retVal, new CommunicationsComparator());
2012-12-13 14: 03: 00.0
2012-12-06 15: 03: 01.0
2012-12-03 12: 03: 01.0
2012-11-28 11: 28: 11.0
2012-10-09 03: 02: 00.0
2012-10-08 13: 02: 02.0
2012-11-26 10: 02: 05.0
2012-10-07 03: 02: 01.0
2012-10-03 10: 02: 02.0
2012-12-28 13: 49: 21.0
2012-12-28 11: 03: 00.0
Any ideas why the bottom two objects might be sorted incorrectly? I am using the MySQL JDBC implementation of this timestamp.
source share