核心代码:
核心代码是JDK的一个工具类,对中文数组的排序
public static void main(String[] args) {
List<String> list1 = Arrays.asList("1月上旬", "1月中旬", "1月下旬", "2月上旬", "2月中旬", "2月下旬");
List<String> list2 = Arrays.asList("1月前旬", "1月中旬", "1月后旬", "2月前旬", "2月中旬", "2月后旬");
Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
Arrays.sort(list1.toArray(), cmp);
for (String s : list1) {
System.out.println("===list1===" + s);
}
}
其中:
Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
可以提供中文排序的依据
拓展:
对List<T>进行排序
- 以对 List<LinkedHashMap<String, Object>> 为例,可对 泛型<T>进行扩展,核心方法是
sortList
public static void main(String[] args) {
List<LinkedHashMap<String, Object>> mapList = fillList();
sortList(mapList, "月份");
for (LinkedHashMap<String, Object> map : mapList) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("==============="+entry.getKey()+"============"+entry.getValue());
}
}
}
//然后 对 List<LinkedHashMap<string,Object>> 进行填充
private static List<LinkedHashMap<String,Object>> fillList(){
List<LinkedHashMap<String,Object>> list = new ArrayList<>();
LinkedHashMap<String, Object> map_1 = new LinkedHashMap<>();
map_1.put("月份","1月上旬");
map_1.put("地址", ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_2 = new LinkedHashMap<>();
map_2.put("月份","1月中旬");
map_2.put("地址",ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_3 = new LinkedHashMap<>();
map_3.put("月份","1月中旬");
map_3.put("地址",ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_4 = new LinkedHashMap<>();
map_4.put("月份","1月下旬");
map_4.put("地址",ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_5 = new LinkedHashMap<>();
map_5.put("月份","2月上旬");
map_5.put("地址",ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_6 = new LinkedHashMap<>();
map_6.put("月份","2月上旬");
map_6.put("地址",ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_7 = new LinkedHashMap<>();
map_7.put("月份","2月中旬");
map_7.put("地址",ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_8 = new LinkedHashMap<>();
map_8.put("月份","2月下旬");
map_8.put("地址",ChineseAddressGenerator.getInstance().generate());
LinkedHashMap<String, Object> map_9 = new LinkedHashMap<>();
map_9.put("月份","2月下旬");
map_9.put("地址",ChineseAddressGenerator.getInstance().generate());
list.add(map_1);
list.add(map_2);
list.add(map_3);
list.add(map_4);
list.add(map_5);
list.add(map_6);
list.add(map_7);
list.add(map_8);
list.add(map_9);
return list;
}
//封装 对 List<LinkedHashMap<string,Object>> 的排序
private static void sortList(List<LinkedHashMap<String,Object>> list,String sortKey){
Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
Collections.sort(list,new Comparator<LinkedHashMap<String,Object>>(){
@Override
public int compare(LinkedHashMap<String, Object> o1, LinkedHashMap<String, Object> o2) {
String key1 = o1.get(sortKey).toString();
String key2 = o2.get(sortKey).toString();
List<String> tempList = Arrays.asList(key1,key2);
Arrays.sort(tempList.toArray(),cmp);
String str = StringUtils.join((String[]) tempList.toArray());
if(Objects.equals(key1,key2)){
return 0;
}else if(str.startsWith(key1)){
return -1;
}else{
return 1;
}
}
});
}