Sort Array List of Objects in Java



Sort Array List of Objects in Java | ninjasquad

In this tutorial, we’ll learn how to sort an Array List of Objects in Java

Build 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

Let’s look at various examples of sorting the library of books:-

Sort in ascending order
  1. Sort a list of book objects by author in ascending order
    Collections.sort(library, Comparator.comparing(Book::getAuthor));
    
    // [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)]
    
  2. Sort a list of book objects by rating in ascending order
    Collections.sort(library, Comparator.comparingDouble(Book::getRating));
    
    // [Book(name=book4, author=author2, rating=2.0), 
    //  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)]
    

Sort in descending order
  1. Sort a list of book objects by author in descending order
    Collections.sort(library, Comparator.comparing(Book::getAuthor).reversed());
    
    // [Book(name=book4, author=author2, rating=2.0), 
    //  Book(name=book2, author=author2, rating=5.0), 
    //  Book(name=book1, author=author1, rating=3.0), 
    //  Book(name=book3, author=author1, rating=4.0), 
    //  Book(name=book5, author=author1, rating=4.0)]
    
  2. Sort a list of book objects by rating in descending order
    Collections.sort(library, Comparator.comparingDouble(Book::getRating).reversed());
    
    // [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 multiple fields in ascending order
  1. Sort a list of book objects by author and then rating in ascending order
    Collections.sort(library, Comparator.comparing(Book::getAuthor)
            .thenComparing(Book::getRating));
    
    // [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)]
    
  2. Sort a list of book objects by author, then rating and then name in ascending order
    Collections.sort(library, Comparator.comparing(Book::getAuthor)
            .thenComparing(Book::getRating)
            .thenComparing(Book::getName));
    
    // [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 multiple fields in ascending then descending order
  1. Sort a list of book objects by author in ascending and then rating in descending order
    Collections.sort(library, Comparator.comparing(Book::getAuthor)
        .thenComparing((b1, b2) -> Double.compare(b2.getRating(), b1.getRating())));
    
    // [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=book2, author=author2, rating=5.0), 
    //  Book(name=book4, author=author2, rating=2.0)]
    
  2. Sort a list of book objects by author in ascending, then rating in descending and then name in descending 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())));
    
    // [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: Internet

Leave a Comment

We are offering free coding tuts

X