为什么s2-s1打印从大到小,s1-s2打印从小到大,请说的详细点
import java.util.Comparator;
import java.util.TreeSet;
public class Text28 {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>(new ComparatorDemo());
treeSet.add("adf");
treeSet.add("asdf");
treeSet.add("adfd");
treeSet.add("asd");
treeSet.add("a");
treeSet.add("ag");
treeSet.add("fdffffffa");
System.out.println(treeSet);
}
}
class ComparatorDemo implements Comparator<String>{
@Override
public int compare(String s1, String s2) {
int num = s2.length()-s1.length(); ////////////////////////////////////为什么s2-s1打印从大到小,s1-s2打印从小到大,请说的详细点
return num == 0 ? s2.compareTo(s1):num;
}
}
|
|