sorting a List of Map

后端 未结 1 1143
天命终不由人
天命终不由人 2020-11-28 12:15

I have a list variable created like this:

List> list = new ArrayList>();

In my

相关标签:
1条回答
  • 2020-11-28 12:22

    The following code works perfectly

    public Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
        public int compare(Map<String, String> m1, Map<String, String> m2) {
            return m1.get("name").compareTo(m2.get("name"));
        }
    }
    
    Collections.sort(list, mapComparator);
    

    But your maps should probably be instances of a specific class.

    0 讨论(0)
提交回复
热议问题