Java - Guava API to implement compareTo with considering sorting ordering

less than 1 minute read

Example of a chained comparison statement.

1
2
3
4
5
6
7
   public int compareTo(Foo that) {
     return ComparisonChain.start()
         .compare(this.aString, that.aString)
         .compare(this.anInt, that.anInt)
         .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
         .result();
   }