我有Enum
课
public enum ServiceTypes {
Zero("zero", 6, true),
AVL("Avl", 1, true),
VPS("vps", 2, true),
CALCULATOR("calculator", 3, true),
SIMULATOR("Simulator", 4, true),
CONTRACT("Contract Output", 5, true),
ALL("all", 7, true);
private static final OrderComparator orderComparator = new OrderComparator();
private static final OrderComparator nameComparator = new OrderComparator();
@Nonnull
public static ServiceTypes[] getOrderedServiceTypes() {
Arrays.sort(values(), orderComparator);
return values();
}
@Nonnull
public static ServiceTypes[] getNamedServiceTypes() {
Arrays.sort(values(), nameComparator);
return values();
}
private static final class OrderComparator implements Comparator<ServiceTypes> {
@Override
public int compare(final ServiceTypes o1, final ServiceTypes o2) {
return o1.getOrder().compareTo(o2.getOrder());
}
}
private static final class AlphabeticalOrder implements Comparator<ServiceTypes> {
@Override
public int compare(final ServiceTypes o1, final ServiceTypes o2) {
return o1.toString().compareTo(o2.toString());
}
}
}
当 I 时test
getOrderedServiceTypes()
,我将Zero("zero", 6, true)
其视为第一个元素,这是不正确的。
似乎values()
不是根据比较器排序的。
如何在 Enum 上运行自定义订单?