JAVA中对List>中的中文汉字进行排序

时间:2020-12-23 08:00:29

本篇文章,对中文的排序规则为:首个汉字——拼音——首字母,遵循26个英文字母的顺序排列

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

public class ListSort {
public static void main(String[] args) {

List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
Map<String, Object> map=new HashMap<String, Object>();
map.put("name", "中国");
Map<String, Object> map1=new HashMap<String, Object>();
map1.put("name", "美国");
Map<String, Object> map2=new HashMap<String, Object>();
map2.put("name", "俄罗斯");
Map<String, Object> map3=new HashMap<String, Object>();
map3.put("name", "英国");

list.add(map);
list.add(map1);
list.add(map2);
list.add(map3);
System.out.println("排序前:"+list);
listSort(list);
System.out.println("排序后:"+list);
}

public static void listSort(List<Map<String, Object>> resultList) {
Collections.sort(resultList, new Comparator<Map<String, Object>>() {

public int compare(Map<String, Object> o1, Map<String, Object> o2) {
String name1=MapUtils.getString(o1, "name");
String name2=MapUtils.getString(o2, "name");
Collator instance = Collator.getInstance(Locale.CHINA);
return instance.compare(name1, name2);

}
});
}
}
结果:
JAVA中对List<Map<String,Object>>中的中文汉字进行排序