Sorting ArrayList of Objects by Object attribute

18,395

Solution 1

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

Related questions

On sorting List on various criteria:

On Comparator and Comparable

Solution 2

Another good way of doing this that is a bit more flexible if there is more than one property of an object that you may wish to sort by is to use Guava's Ordering class with its onResultOf(Function) option. This is ideally suited for sorting by properties since a Function can be used to retrieve and return a specific property of an object. For a simple example, imagine a class Person with String getFirstName() and String getLastName() methods.

List<Person> people = ...;
Collections.sort(people, Ordering.natural().onResultOf(
    new Function<Person, String>() {
      public String apply(Person from) {
        return from.getFirstName();
      }
    }));

The above will sort the list by first name.

To make it read nicer, you may want to define the functions you might want to use as public static final fields on the Person class. Then you could sort by last name like this:

Collections.sort(people, Ordering.natural().onResultOf(Person.GET_LAST_NAME));

As a fun aside note, this will all be a lot easier in Java 8 with lambda expressions and method references. You'll be able to write something like this without having to define any clumsy anonymous inner classes or static final fields:

import static java.util.Comparator.comparing;
...

people.sort(comparing(Person::getLastName));
Share:
18,395
Vishal
Author by

Vishal

SDE 2 @Amazon

Updated on July 23, 2022

Comments

  • Vishal
    Vishal almost 2 years

    I am having an Arraylist of Objects. Those object have an attribute or datatype - 'String'. I need to sort the Arraylist by that string. How to achieve this?

  • Vishal
    Vishal almost 14 years
    Thanks! :) Can you elaborate more about the use of comparator and how it will return me sorted objects according to string. Note that I need to sort them in alphabetical order of String.
  • Vishal
    Vishal almost 14 years
    Oh. Got the idea about comparator. But I cant implement this interface on the Object because of some other issues. So I need to make Comparator object.
  • polygenelubricants
    polygenelubricants almost 14 years
    @Vishal: look at the linked question (Sorting an ArrayList of Contacts). It has the examples you're looking for and more. Also, Effective Java 2nd Edition, Item 12: Consider implementing Comparable
  • Stefan Endrullis
    Stefan Endrullis over 10 years
    You're right. It should have been much easier in Java 8 with lambda expression. Unfortunately Sun and Oracle missed again the opportunity to add some high-level methods like sortBy to collections and arrays. So a trivial Scala line of code ("people.sortBy(_.lastName)") is still translated into a four times longer Java line of code. That's sad. But anyway, thanks for this Guava shortcut with the Ordering class. It's still useful in Java 8.
  • ColinD
    ColinD over 10 years
    @StefanEndrullis: With one static import, the same thing in Java 8 looks like this: people.sort(comparing(Person::lastName)). Seems like you're underselling it a bit.
  • Stefan Endrullis
    Stefan Endrullis over 10 years
    OK, your last edit by statically importing comparing makes it also quite short and readable. Thanks for the tip.