关于用sort对list排序不生效的问题

问题描述:

 List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("age","58");
        Map<String,Object> map2 = new HashMap<>();
        map2.put("age","58999998");
        Map<String,Object> map3 = new HashMap<>();
        map3.put("age","99999");
        list.add(map1);
        list.add(map2);
        list.add(map3);
       // System.out.println(list);
         list.sort(Comparator.comparing(o -> o.get("age").toString()));
        System.out.println(list);

结果
[{age=58}, {age=58999998}, {age=99999}]

value值对应的是字符串类型,按照字典顺序比较。
值换成Integer类型试试。


 List<Map<String,Integer>> list = new ArrayList<>();
        Map<String,Integer> map1 = new HashMap<>();
        map1.put("age",58);
        Map<String,Integer> map2 = new HashMap<>();
        map2.put("age",58999998);
        Map<String,Integer> map3 = new HashMap<>();
        map3.put("age",99999);
        list.add(map1);
        list.add(map2);
        list.add(map3);
       // System.out.println(list);
         list.sort(Comparator.comparing(o -> o.get("age")));
        System.out.println(list);

sort排序主要是针对int数组进行排序,其他类型的排序,必须在类里面实现Comparator接口。

但是我需要用字符类型怎么办

感谢各位,太感谢了

需要覆写compare 和hashcode方法的;