Sort ArrayList of custom Objects by property
Since Date
implements Comparable
, it has a compareTo
method just like String
does.
So your custom Comparator
could look like this:
public class CustomComparator implements Comparator<MyObject> {
@Override
public int compare(MyObject o1, MyObject o2) {
return o1.getStartDate().compareTo(o2.getStartDate());
}
}
The compare()
method must return an int
, so you couldn't directly return a boolean
like you were planning to anyway.
Your sorting code would be just about like you wrote:
Collections.sort(Database.arrayList, new CustomComparator());
A slightly shorter way to write all this, if you don't need to reuse your comparator, is to write it as an inline anonymous class:
Collections.sort(Database.arrayList, new Comparator<MyObject>() {
@Override
public int compare(MyObject o1, MyObject o2) {
return o1.getStartDate().compareTo(o2.getStartDate());
}
});
Since java-8
You can now write the last example in a shorter form by using a lambda expression for the Comparator
:
Collections.sort(Database.arrayList,
(o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));
And List
has a sort(Comparator)
method, so you can shorten this even further:
Database.arrayList.sort((o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));
This is such a common idiom that there's a built-in method to generate a Comparator
for a class with a Comparable
key:
Database.arrayList.sort(Comparator.comparing(MyObject::getStartDate));
All of these are equivalent forms.
Sort arraylist of objects by one of their properties (double value)
Using Collections, you can do it like this
Collections.sort(bounties, new Comparator<BountyObject>() {
@Override public int compare(BountyObject bo1, BountyObject bo2) {
return (bo1.getAmount() > bo2.getAmount() ? 1:-1);
}
Sorting an arraylist of object base on its property in custom order
Unless you actually try to sort a list containing the items listed in the parameters of Ordering.explicit
, it doesn't know how to sort them. It's also not clear if you've overridden your WeekSales.equals
(and hashCode
), which would be required for that to work too.
You need to actually tell it the fields to compare:
class YourComparator implements Comparator<WeekSales> {
private final Ordering<String> dayOrdering = Ordering.explicit(
"Monday", "Tuesday", ... );
@Override public int compare(WeekSales a, WeekSales b) {
return dayOrdering.compare(a.getDay(), b.getDay());
}
}
Sorting ArrayList of Objects by Object attribute
You need to write a Comparator<MyObject>
and use Collections.sort(List<T>, Comparator<? super T>
to sort your List
.
Or else, your MyObject
can also implements Comparable<MyObject>
, defining a natural ordering that compares on your specific attribute, and then use Collections.sort(List<T>
instead.
See also
- Java Tutorials/Object Ordering
Related questions
On sorting List
on various criteria:
- Sorting an ArrayList of Contacts
On Comparator
and Comparable
- When to use Comparable vs Comparator
- difference between compare() and compareTo()
- Comparable and Comparator contract with regards to null
- Why does the Java Collections Framework offer two different ways to sort?
Related Topics
Eclipse: Attach Source/Javadoc to a Library via a Local Property
Maven Is Not Working in Java 8 When Javadoc Tags Are Incomplete
Java Generate Random Number Between Two Given Values
Configure Hibernate (Using JPA) to Store Y/N for Type Boolean Instead of 0/1
Multipart File Upload Using Spring Rest Template + Spring Web MVC
Get All of the Classes in the Classpath
How to Add Checkboxes to Jtable Swing
Installed Java 7 on MAC Os X But Terminal Is Still Using Version 6
Configuring Spring Security 3.X to Have Multiple Entry Points
Performance of Traditional for Loop VS Iterator/Foreach in Java
Adding Image to Jbutton with Foreground Label
Executors.Newcachedthreadpool() Versus Executors.Newfixedthreadpool()
How to Split String with Some Separator But Without Removing That Separator in Java
Increase the Java Heap Size Permanently
Java for Loop Syntax: "For (T Obj:Objects)"