Date:

Share:

Sort Array List of Objects in Java

Related Articles

In this tutorial, we will learn how to sort an array list of objects in Java

Building a list of objects

Let’s understand the sorting of an array list of objects with an example. Let’s create a class Book:-

@Data
public class Book {
  private final String name;
  private final String author;
  private final double rating;
}

Let’s create some Book objects and build a library with an array list of book objects:-

Book book1 = new Book("book1", "author1", 3.0);
Book book2 = new Book("book2", "author2", 5);
Book book3 = new Book("book3", "author1", 4.0);
Book book4 = new Book("book4", "author2", 2.5);
Book book5 = new Book("book5", "author1", 4.0);

List<Book> library = Arrays.asList(book1, book2, book3, book4, book5);

Sort list of objects using Collections.sort()

we are using Collections.sort() A method to sort the existing list of objects using Comparators. This sort occurs on the same list of objects and no new list is created. If you want to keep the existing list as it is after sorting, use Streams instead, which creates a new sorted list.

Sort in ascending order

Sort a list of objects in the book by author in ascending order (author name in alphabetical order)

Collections.sort(library, Comparator.comparing(Book::getAuthor));

System.out.println(library);
// [Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book2, author=author2, rating=5.0), 
//  Book(name=book4, author=author2, rating=2.0)]

Sort in descending order

Sort a list of objects in the book by rank in descending order (rank number in descending order)

Collections.sort(library, Comparator.comparingDouble(Book::getRating).reversed());

System.out.println(library);
// [Book(name=book2, author=author2, rating=5.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book4, author=author2, rating=2.0)]

Sort by number of fields in ascending order

Sort a list of objects in a book first by author name in ascending alphabetical order, then by rank in ascending order, then by book title in alphabetical order.

Collections.sort(library, Comparator.comparing(Book::getAuthor)
        .thenComparing(Book::getRating)
        .thenComparing(Book::getName));

System.out.println(library);
// [Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book4, author=author2, rating=2.0), 
//  Book(name=book2, author=author2, rating=5.0)]

Sort by number of fields in ascending and then descending order

Sort a list of objects in a book by author name in ascending alphabetical order, then rank in descending order, then by book title in descending alphabetical order.

Collections.sort(library, Comparator.comparing(Book::getAuthor)
    .thenComparing((b1, b2) -> Double.compare(b2.getRating(), b1.getRating()))
    .thenComparing((b1, b2) -> b2.getName().compareTo(b1.getName())));

System.out.println(library);
// [Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book2, author=author2, rating=5.0), 
//  Book(name=book4, author=author2, rating=2.0)]

Sort list of objects using streams

Use Java Streams to sort the list of objects when you don’t want to modify the existing list and create a new sorted list instead.

Sort in ascending order

Sort a list of objects in a book by author in ascending order

List<Book> sortedLibrary = library.stream()
      .sorted(Comparator.comparing(Book::getAuthor))
      .collect(Collectors.toList());

System.out.println(sortedLibrary);
// [Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book2, author=author2, rating=5.0), 
//  Book(name=book4, author=author2, rating=2.0)]

Sort in descending order

Sort a list of objects in the book by ranking in descending order

List<Book> sortedLibrary = library.stream()
      .sorted(Comparator.comparingDouble(Book::getRating)
      .reversed())
      .collect(Collectors.toList());

System.out.println(sortedLibrary);
// [Book(name=book2, author=author2, rating=5.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book4, author=author2, rating=2.0)]

Sort by number of fields in ascending order

Sort a list of objects in a book by author, then rating, then name in ascending order

List<Book> sortedLibrary = library.stream()
      .sorted(Comparator.comparing(Book::getAuthor)
      .thenComparingDouble(Book::getRating)
      .thenComparing(Book::getName))
      .collect(Collectors.toList())

System.out.println(sortedLibrary);
// [Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book4, author=author2, rating=2.0), 
//  Book(name=book2, author=author2, rating=5.0)]

Sort by number of fields in ascending and then descending order

Sort a list of objects in a book by author in ascending order, then rank in descending order, then name in descending order

List<Book> sortedLibrary = library.stream()
      .sorted(Comparator.comparing(Book::getAuthor)
      .thenComparing((b1, b2) -> Double.compare(b2.getRating(), b1.getRating()))
      .thenComparing((b1, b2) -> b2.getName().compareTo(b1.getName())))
      .collect(Collectors.toList())

System.out.println(sortedLibrary);
// [Book(name=book5, author=author1, rating=4.0), 
//  Book(name=book3, author=author1, rating=4.0), 
//  Book(name=book1, author=author1, rating=3.0), 
//  Book(name=book2, author=author2, rating=5.0), 
//  Book(name=book4, author=author2, rating=2.0)]

Source

Popular Articles