Close

Java Collections - Comparator.thenComparing() Examples

Java Collections Java Java API 


Interface:

java.util.Comparator

java.util.ComparatorComparatorLogicBig

Methods:

default Comparator<T> thenComparing(Comparator<? super T> other)

This default method returns a comparator with this comparator and the specified comparator are chained together. If this Comparator considers two elements equal, i.e. compare(a, b) == 0, then the other specified comparator is used to determine the order.



default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor)

This default method returns a comparator chained with this comparator. The returned comparator uses the specified function to extract the Comparable sort key.


Examples


package com.logicbig.example.comparator;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ThenComparingExample {

public static void main(String... args) {
System.out.println("-- without thenComparing --");
withoutThenComparing();
System.out.println("-- with thenComparing --");
withThenComparing();
}


private static void withoutThenComparing() {
List<Book> list = createExampleBooks();
System.out.printf("before sort: %s%n", list);
Collections.sort(list, Comparator.comparing(Book::getAuthor));
System.out.printf("after sort: %s%n", list);
}


private static void withThenComparing() {
List<Book> list = createExampleBooks();
System.out.printf("before sort: %s%n", list);
Collections.sort(list, Comparator.comparing(Book::getAuthor)
.thenComparing(Comparator.comparing(Book::getName)));
System.out.printf("after sort: %s%n", list);
}

private static List<Book> createExampleBooks() {
return Arrays.asList(
new Book("Sara", "book1"),
new Book("Sara", "book3"),
new Book("Sara", "book2"),
new Book("John", "book5"),
new Book("John", "book4")
);
}

private static class Book {
private String author;
private String name;

public Book(String author, String name) {
this.author = author;
this.name = name;
}

public String getAuthor() {
return author;
}

public String getName() {
return name;
}

@Override
public String toString() {
return author + " - " + name;
}
}
}

Output

-- without thenComparing --
before sort: [Sara - book1, Sara - book3, Sara - book2, John - book5, John - book4]
after sort: [John - book5, John - book4, Sara - book1, Sara - book3, Sara - book2]
-- with thenComparing --
before sort: [Sara - book1, Sara - book3, Sara - book2, John - book5, John - book4]
after sort: [John - book4, John - book5, Sara - book1, Sara - book2, Sara - book3]




Using KeyExtractor function

package com.logicbig.example.comparator;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ThenComparingExample2 {

public static void main(String... args) {
List<Book> list = createExampleBooks();
System.out.printf("before sort: %s%n", list);
Collections.sort(list, Comparator.comparing(Book::getAuthor)
.thenComparing(Book::getName));
System.out.printf("after sort: %s%n", list);
}

private static List<Book> createExampleBooks() {
return Arrays.asList(
new Book("Sara", "book1"),
new Book("Sara", "book3"),
new Book("Sara", "book2"),
new Book("John", "book5"),
new Book("John", "book4")
);
}

private static class Book {
private String author;
private String name;

public Book(String author, String name) {
this.author = author;
this.name = name;
}

public String getAuthor() {
return author;
}

public String getName() {
return name;
}

@Override
public String toString() {
return author + " - " + name;
}
}
}

Output

before sort: [Sara - book1, Sara - book3, Sara - book2, John - book5, John - book4]
after sort: [John - book4, John - book5, Sara - book1, Sara - book2, Sara - book3]




Using streams:

package com.logicbig.example.comparator;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ThenComparingExample3 {
public static void main(String... args) {
//original list
List<String> list = Arrays.asList("abcd3", "ac2", "abcd1", "ac1");
System.out.println(list);

//sorting by string length
List<String> list2 = list.stream().sorted(Comparator.comparing(String::length))
.collect(Collectors.toList());
System.out.println(list2);

//sorting by string length and then by natural order
List<String> list3 = list.stream().sorted(
Comparator.comparing(String::length)
.thenComparing(Comparator.naturalOrder()))
.collect(Collectors.toList());
System.out.println(list3);
}
}

Output

[abcd3, ac2, abcd1, ac1]
[ac2, ac1, abcd3, abcd1]
[ac1, ac2, abcd1, abcd3]




See Also