Java - Java 8 Comparator to replace Guava compound comparing

less than 1 minute read

Guava example

1
2
3
Collection.sort(list,
    Ordering.from((Comparator<Item>) Comparator.comparingInt(item -> item.getId()))
    .compound(Comparator.comparing(Item::getName)))

Java 8 example

1
2
3
4
Comparator<Item> compoundComparator
      = Comparator.comparingInt(Item::getId)
        .thenComparing(Item::getName);
Collection.sort(list, compoundComparator)