我正在尝试使用ComparisonChain在类中实现compareTo(),但是该类包含List,而compare()不接受它们,因为List没有实现Comparable。关于如何使这项工作的任何想法?
代码子集看起来像这样:
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();
}
}
答案 0 :(得分:13)
ComparisonChain.start()
.compare(this.name, that.name)
.compare(this.emails, that.emails,
Ordering.<String>natural().lexicographical())
.result();