今天敲代码遇到一个问题:需要对list的某个字段进行排序,而且需要根据自己定义的顺序进行排序;
我的需求中根据Index实体类的indexType字段进行排序,顺序为:B,D,C,A;
啥都不会的我只能百度,后来解决:
String[] : 我的indexType字段为String类型,数组中为我自定义的顺序(用的常量)
new FixedOrderComparator(string[]) 数组作为排序的方式
new BeanComparator("indexType",TypeComparator) list中的indexType的属性作为排序的字段
Collections.sort(list,indexTypeComparator); list进行排序 按照indexType字段以自己定义的数组的顺序进行排序。