排序規則枚舉:
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()); } });