Date Sorting - Latest to Oldest
Collections.sort(someList, new Comparator<SomeObject>() {
public int compare(final SomeObject object1, final SomeObject object2) {
return (object1.getS开发者_JS百科omeDate()).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?
Comparator.comparing
You can pass a method reference to Comparator.comparing
.
If you want the objects to be sorted in ascending order based on the date:
someList.sort(Comparator.comparing(SomeObject::getSomeDate));
or
someList.sort(Comparator.comparing(SomeObject::getSomeDate).reversed());
for descending order.
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;
}}
);
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();
}
});
This is old but may be someone can use it. It may be sorted using java8 as follows:
someList.sort(Comparator.comparing(listMember::dateProducingMethod))
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?
Try this:
List<Date> list=new ArrayList<Date>();
//add some dates to list
Collections.sort(list, new Comparator<Date>() {
public int compare(final Date object1, final Date object2) {
return Long.compare(object1.getTime(),object2.getTime());
}}
);
Date.getTime() "converts" the date to a long, which is easier to compare and sort.
Anyway behind the curtain Longs are compared with this:
public static int compare(long x, long y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
If you want to invert the sort, just multiply by -1 like this:
List<Date> list=new ArrayList<Date>();
//add some dates to list
Collections.sort(list, new Comparator<Date>() {
public int compare(final Date object1, final Date object2) {
return Long.compare(object1.getTime(),object2.getTime())*-1;
}}
);
精彩评论