更新时间:2022-05-24 16:37:55
您应该简单地委托已经提供的枚举compareTo()方法,并反映声明顺序(基于 ordinal
value):
You should simply delegate to the enum compareTo() method which is already provided and reflects the declaration order (based on the ordinal
value):
Collections.sort(list, new Comparator() {
@Override
public int compare(A a1, A a2) {
return a1.getType().compareTo(a2.getType());
}
});
或者,如果您认为组件类型为您的元素提供自然顺序,您可以使A类本身实现 Comparable
,并将 compareTo
方法委托给 ComponentType
一。
Or, if you think that the component type provides the "natural order" for your elements, you can make the A class itself implement Comparable
and also delegate the compareTo
method to the ComponentType
one.