views:

94

answers:

4
Collections.sort(someList, new Comparator<SomeObject>() {
            public int compare(final SomeObject object1, final SomeObject object2) {
                return (object1.getSomeDate()).compareTo(object2.getSomeDate()); 
            }}
        );

Would it give me the objects with latest dates meaning the list will contain the set of objects with latest date to oldest date?

+2  A: 

To be sure you can use:

Collections.sort(someList, new Comparator<SomeObject>() {
        public int compare(final SomeObject object1, final SomeObject object2) {
            return object1.getSomeDate().after(object2.getSomeDate()) ? 1 : -1; 
        }}
);
Roman
Wouldn't this violate the Comparable contract regarding equal elements? For two equal date objects both compare(date1, date2) and compare(date2, date1) would return -1 in your implementation.
Jörn Horstmann
@Jörn Horstmann: theoretically it would but in practice I'm not sure that this is the case where it's worth to add one more (almost same) verification just to get the same result.
Roman
-1 Very bad idea to break the Comparator spec.
Kevin Bourrillion
@Kevin Bourrillion: can you give more meaningful arguments?
Roman
A: 

If it sorts in the wrong order, you can add simply a minus sign just after return so: return -object1.getSom......

Martijn Courteaux
Or, more easily, swapping the -1 and 1.
lavinio
-1 Negation is not a good idea here, as there exists a negative number whose negative is also negative!
Kevin Bourrillion
+1  A: 

The default ordering of Date will put newer dates after older dates so the oldest dates would be at the beginning of your list and the newest dates at the end. Comparators have always been hard to read in my opinion so I have switched to using google's Ordering objects that implement Comparator a little cleaner. For example your Comparator could be written like this:

Ordering<SomeObject> order = Ordering.natural().onResultOf(new Function<SomeObject, Date>() {
    public Date apply(SomeObject object) {
        return object.getDate();
    }
});
Comparator<SomeObject> comparator = order; // Ordering implements Comparable so this would be legal to do
Collections.sort(someList, order);

The order Comparator that this code created would sort SomeObject objects based on their Date using the Date's natural ordering. But what makes Ordering really nice is some of extra methods change the order without having to write any more logic, for example to reverse the order of dates to be newest to oldest you just have to add a call to reverse():

Ordering<SomeObject> order = Ordering.natural().reverse().onResultOf(new Function<SomeObject, Date>() {
    public Date apply(SomeObject object) {
        return object.getDate();
    }
});
Yanamon
the only thing I don't understand here is why you have the 'comparator' temporary variable. You don't use it or need it.Note that as well as `Collections.sort(someList, order)` you could alternatively do `List<SomeObject> newList = order.sortedCopy(someList)`.
Kevin Bourrillion
That was mostly done clearly show that Ordering<SomeObject> implemented Comparator<SomeObject>, but thanks for the comment about sortedCopy I hadnt noticed that method before.
Yanamon
A: 

By using lambdaj you could achieve the same result in an easier and more readable way as it follows:

sort(someList, on(SomeObject.class).getSomeDate());

Far better than writing an obscure inner class, isn't it?

Mario Fusco