问题:
有一个乱序的对象集合,要求先按对象的属性A排序(排序规则由业务确定,非A-Z或0-9的常规顺序),相同A属性的记录,按根据属性B排序(排序规则,同样由业务确定,非常规顺序) -前提:业务规则是明确的
示例:
FSUData对象定义
1 package acc.test; 2 3 public class FSUData { 4 5 public FSUData(String airport, String fsuCode) { 6 this.airport = airport; 7 this.fsuCode = fsuCode; 8 } 9 10 /** 11 * 航站 12 */ 13 private String airport; 14 15 /** 16 * FSU状态节点 17 */ 18 private String fsuCode; 19 20 public String getAirport() { 21 return airport; 22 } 23 24 public void setAirport(String airport) { 25 this.airport = airport; 26 } 27 28 public String getFsuCode() { 29 return fsuCode; 30 } 31 32 public void setFsuCode(String fsuCode) { 33 this.fsuCode = fsuCode; 34 } 35 36 public String toString() { 37 return airport + "/" + fsuCode; 38 } 39 40 }
View Code
原始数据:
[法兰克福/RCF, 法兰克福/DLV, 成都/DEP, 成都/RCS, 上海/DEP, 上海/RCF]
业务规则:
航站排序规则:成都 -> 上海 -> 法兰克福
FSU排序规则:RCS -> RCF -> TFD -> DEP -> DLV
要求排序后变成下面这样:
[成都/RCS, 成都/DEP, 上海/RCF, 上海/DEP, 法兰克福/RCF, 法兰克福/DLV]
java代码:
1 package acc.test; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 import org.junit.Test; 11 12 public class FSUSortTest { 13 14 Map<String, Integer> airportDic = new HashMap<String, Integer>(); 15 Map<String, Integer> fsuDic = new HashMap<String, Integer>(); 16 17 @Test 18 public void test() { 19 20 // 先定义顺序规则 21 airportDic.put("成都", 1); 22 airportDic.put("上海", 2); 23 airportDic.put("法兰克福", 3); 24 25 fsuDic.put("RCS", 1); 26 fsuDic.put("RCF", 2); 27 fsuDic.put("TFD", 3); 28 fsuDic.put("DEP", 4); 29 fsuDic.put("DLV", 5); 30 31 // 建一个待排序的目标"数组" 32 List<FSUData> target = new ArrayList<FSUData>(); 33 34 target.add(new FSUData("法兰克福", "RCF")); 35 target.add(new FSUData("法兰克福", "DLV")); 36 37 target.add(new FSUData("成都", "DEP")); 38 target.add(new FSUData("成都", "RCS")); 39 40 target.add(new FSUData("上海", "DEP")); 41 target.add(new FSUData("上海", "RCF")); 42 43 System.out.println("排序前:" + target); 44 45 // 排序 46 FsuComparator fsuComparator = new FsuComparator(airportDic, fsuDic); 47 Collections.sort(target, fsuComparator); 48 49 System.out.println("排序后:" + target); 50 } 51 52 private class FsuComparator implements Comparator<FSUData> { 53 54 Map<String, Integer> airportMap; 55 Map<String, Integer> fsuMap; 56 57 public FsuComparator(Map<String, Integer> airportMap, 58 Map<String, Integer> fsuMap) { 59 this.airportMap = airportMap; 60 this.fsuMap = fsuMap; 61 } 62 63 public int compare(FSUData s1, FSUData s2) { 64 65 // 从顺序规则Map里,取出"排序值" 66 Integer airportOrder1 = airportMap.get(s1.getAirport()); 67 Integer airportOrder2 = airportMap.get(s2.getAirport()); 68 69 // 如果Airport相同,则比较FSUCode 70 if (airportOrder1 == airportOrder2) { 71 Integer fsuOrder1 = fsuMap.get(s1.getFsuCode()); 72 Integer fsuOrder2 = fsuMap.get(s2.getFsuCode()); 73 return fsuOrder1.compareTo(fsuOrder2); 74 } 75 76 return airportOrder1.compareTo(airportOrder2); 77 } 78 } 79 80 }
View Code
思路:先将"业务排序规则" 转换成 "常规有序规则",然后再利用Comparator接口实现比较
时间: 2024-11-13 06:43:29