排序规则枚举:
public enum MonitorComponentTypeEnum { /** 设备工况 **/ APPARATUS_WORKING_CONDITION("00000000000000000000000000030101","设备工况",4), /** 外部环境 **/ EXTERNAL_ENVIRONMENT("00000000000000000000000000030102","外部环境",5), /** 测点工况 **/ ANCHOR_WORKING_CONDITION("00000000000000000000000000030103","测点工况",1), /** 组合分析 **/ GROUP_ANALYSE("00000000000000000000000000030104","组合分析",2), /** 综合分析 **/ COMPREHENSIVE_EARLY_WARNING("00000000000000000000000000030105","综合分析",3); /**id**/ private String id; /**中文描述**/ private String desc; /**排序**/ private Integer sort; MonitorComponentTypeEnum(String id, String desc, Integer sort) { this.id = id; this.desc = desc; this.sort = sort; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public Integer getSort() { return sort; } public void setSort(Integer sort) { this.sort = sort; } public static MonitorComponentTypeEnum getAppCategoryEnum(String id){ for (MonitorComponentTypeEnum e : values()) { String currId=e.getId(); if(currId.equals(id)){ return e; } } return null; } public static List<String> typeList(){ List<MonitorComponentTypeEnum> list = new ArrayList<>(); for (MonitorComponentTypeEnum e : MonitorComponentTypeEnum.values()) { list.add(e); } Collections.sort(list, new Comparator<MonitorComponentTypeEnum>() { @Override public int compare(MonitorComponentTypeEnum o1, MonitorComponentTypeEnum o2) { return o1.sort-o2.sort; } }); List<String> idList = list.stream().map(MonitorComponentTypeEnum::getId).collect(Collectors.toList()); return idList; } }
对另一个集合进行排序:
List<MonitorComponentDTO> monitorComponentDTOList = dataResult.getData().stream().filter(a -> monitorComponentIds.contains(a.getMonitorComponentId())).collect(Collectors.toList()); //根据监测项类型排序 List<String> monitorComponentTypeIdList = MonitorComponentTypeEnum.typeList(); Collections.sort(monitorComponentDTOList, new Comparator<MonitorComponentDTO>() { @Override public int compare(MonitorComponentDTO o1, MonitorComponentDTO o2) { return monitorComponentTypeIdList.indexOf(o1.getMonitorComponentTypeId())-monitorComponentTypeIdList.indexOf(o2.getMonitorComponentTypeId()); } });