Guava ComparisonChain Wtih List?

I am trying to use ComparisonChain to implement compareTo () in a class, but the class contains List and compare () does not accept them, because List does not implement Comparable. Any ideas on how to make this work?

A subset of the code looks something like this:

public class User() {
  String name;
  List<String> emails;

  ...

  public int compareTo(User that) {
    return ComparisonChain().start()
                            .compare(this.name, that.name)
                            .compare(this.emails, that.emails)  // Fails on this line
                            .result();
  }
}
+5
source share
1 answer
ComparisonChain.start()
    .compare(this.name, that.name)
    .compare(this.emails, that.emails,
       Ordering.<String>natural().lexicographical())
    .result();
+12
source

All Articles