Collection.sort自定義排序的使用方法
阿新 • • 發佈:2019-02-16
public static void main(String[] args){
Map<String,String> resultMap=new HashMap<String,String>();
resultMap.put("aa", "2");
resultMap.put("bb", "4");
resultMap.put("cc", "1");
List<Map.Entry<String, String>> list= new ArrayList<Map.Entry<String, String>>(resultMap.entrySet());
Map<String, Long> result=new LinkedHashMap<String, Long>(list.size());
long total=0;
try{
Collections.sort(list, new Comparator<Map.Entry<String,String>>(){
public int compare(Map.Entry<String,String> map1,Map.Entry<String,String> map2){
//降序排列
return Long.valueOf(map2.getValue()).compareTo(Long.valueOf(map1.getValue()));
Map<String,String> resultMap=new HashMap<String,String>();
resultMap.put("aa", "2");
resultMap.put("bb", "4");
resultMap.put("cc", "1");
List<Map.Entry<String, String>> list= new ArrayList<Map.Entry<String, String>>(resultMap.entrySet());
Map<String, Long> result=new LinkedHashMap<String, Long>(list.size());
long total=0;
try{
Collections.sort(list, new Comparator<Map.Entry<String,String>>(){
public int compare(Map.Entry<String,String> map1,Map.Entry<String,String> map2){
//降序排列
return Long.valueOf(map2.getValue()).compareTo(Long.valueOf(map1.getValue()));