对list根据某个字段进行排序

今天敲代码遇到一个问题:需要对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字段以自己定义的数组的顺序进行排序。

猜你喜欢

转载自blog.csdn.net/qq_38130280/article/details/79416483