java ArrayList按照枚举排序
Posted zhangjin1120
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java ArrayList按照枚举排序相关的知识,希望对你有一定的参考价值。
- 根据枚举的顺序排序数据:
public class EnumSortTest {
public enum WorkType {
A, B, C;
}
public static class Work {
public String workName;
public WorkType type;
public Work(String workName, WorkType type) {
this.workName = workName;
this.type = type;
}
}
public static void main(String[] args) {
ArrayList<Work> workList = new ArrayList<>();
workList.add(new Work("x1", WorkType.C));
workList.add(new Work("x2", WorkType.B));
workList.add(new Work("x3", WorkType.A));
workList.add(new Work("x4", WorkType.C));
Collections.sort(workList, new Comparator<Work>() {
@Override
public int compare(Work o1, Work o2) {
return o1.type.compareTo(o2.type);
}
});
for (Work w : workList) {
System.out.println(w.workName);
}
}
}
以上是关于java ArrayList按照枚举排序的主要内容,如果未能解决你的问题,请参考以下文章
根据字符串数组顺序对 ArrayList 重新排序 - Java