How to pass a custom comparator to “sort”?

Define your own <=>, and include Comparable. This is from the Comparable doc: class SizeMatters include Comparable attr :str def <=>(an_other) str.size <=> an_other.str.size end def initialize(str) @str = str end def inspect @str end end s1 = SizeMatters.new(“Z”) s2 = SizeMatters.new(“YY”) s3 = SizeMatters.new(“XXX”) s4 = SizeMatters.new(“WWWW”) s5 = SizeMatters.new(“VVVVV”) s1 < s2 #=> … Read more

Comparable and Comparator contract with regards to null

Comparable doesn’t allow null simply because: a.compareTo(b) == -b.compareTo(a) for all objects a and b where !a.equals(b). More specifically: a.equals(b) ? b.equals(a) && a.compareTo(b) == 0 && b.compareTo(a) == 0 && a.hashCode() == b.hashCode() : !b.equals(a) && a.compareTo(b) != 0 && a.compareTo(b) == -b.compareTo(a) must evaluate to true to satisfy the relevant contracts. So null … Read more

Comparator.nullsLast does not avoid NullPointerException

You should use Comparator.nullsLast twice: list.sort(nullsLast(comparing(Bean::getVal, nullsLast(naturalOrder())))); First nullsLast will handle the cases when the Bean objects are null. Second nullsLast will handle the cases when the return value of Bean::getVal is null. In case you’re sure there aren’t any null values in your list then you can omit the first nullsLast (as noted by … Read more

Java TreeMap Comparator

You can not sort TreeMap on values. A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used You will need to provide comparator for Comparator<? super K> so your comparator should compare … Read more

How can Comparator be a Functional Interface when it has two abstract methods? [duplicate]

The docs also state: If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere. And since equals is one of those methods, the “abstract method … Read more

Reverse a comparator in Java 8

You can use Comparator.reverseOrder() to have a comparator giving the reverse of the natural ordering. If you want to reverse the ordering of an existing comparator, you can use Comparator.reversed(). Sample code: Stream.of(1, 4, 2, 5) .sorted(Comparator.reverseOrder()); // stream is now [5, 4, 2, 1] Stream.of(“foo”, “test”, “a”) .sorted(Comparator.comparingInt(String::length).reversed()); // stream is now [test, foo, … Read more

Comparator.reversed() does not compile using lambda

This is a weakness in the compiler’s type inferencing mechanism. In order to infer the type of u in the lambda, the target type for the lambda needs to be established. This is accomplished as follows. userList.sort() is expecting an argument of type Comparator<User>. In the first line, Comparator.comparing() needs to return Comparator<User>. This implies … Read more

Comparator.comparing(…) of a nested field

You can’t nest method references. You can use lambda expressions instead: return Comparator .comparing(l->l.getCourse().getTeacher().getAge(), Comparator.reverseOrder()) .thenComparing(l->l.getCourse().getStudentSize()); Without the need for reverse order it’s even less verbose: return Comparator .comparing(l->l.getCourse().getTeacher().getAge()) .thenComparing(l->l.getCourse().getStudentSize()); Note: in some cases you need to explicitly state the generic types. For example, the code below won’t work without the <FlightAssignment, LocalDateTime> before comparing(…) … Read more

What’s the sort order of Java’s Collections.sort(list, comparator)? small to big or big to small?

The sort order is always ascending, where the Comparator defines which items are larger than others. From the documentation for Collections.sort(List<T> list, Comparator<? super T> c): Sorts the specified list according to the order induced by the specified comparator. From the documentation for Comparator.compare(T,T): Compares its two arguments for order. Returns a negative integer, zero, … Read more