简体   繁体   中英

How to sort java List<Map<String, Object>> by String asc?

My java code is this:

List<SysUser> sysUserDeleteDuplicate = new ArrayList<SysUser>(new HashSet<SysUser>(sysUser));
List<Map<String, Object>> rList = new ArrayList<Map<String, Object>>();
if(sysUserDeleteDuplicate!=null && sysUserDeleteDuplicate.size()>0){
    for (int i=0; i<sysUserDeleteDuplicate.size(); i++){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("text", sysUserDeleteDuplicate.get(i).getRealname());
        resultMap.put("value", sysUserDeleteDuplicate.get(i).getId());
        rList.add(resultMap);
    }
}

I want to sort elements in rList by String ascending. And the data in rList likes this:

[{text=peter,value=1001},{text=lucy,value=1004},{text=kate,value=1002}]

I want this result:

[{text=kate,value=1002},{text=lucy,value=1004},{text=peter,value=1001}]

As each list element is a Map potentially containing more than one instance of Object , you first need to re-define your sort criteria.

It would work if you say that you want to sort the list by the greatest Object in the Map .

But as java.lang.Object does not define java.lang.Comparable<Object> , there is no natural order for instances of Object . So you need to define a sorting criteria for Object , too. As there are not that many attributes, only the hash code or the result of Object.toString() would work.

With these informations, you can implement a Comparator that can be used as argument to List.sort() .

You'll need to make a custom comparator (some sorting criteria) for this. Something like:

Collections.sort(rListMap, new Comparator<Map<String, Comparable>> () {
    @Override
    public int compare(Map<String, Comparable> a1, Map<String, Comparable> a2) {
        return a2.get(someThing).compareTo(a1.get(someThing));
    }
});

As Shubham and tquadrat said,I modified List<Map<String, Object>> to
List<Map<String, String>>
I use this code:

List<SysUser> sysUserDeleteDuplicate = new ArrayList<SysUser>(new HashSet<SysUser>(sysUser));
List<Map<String, String>> rList = new ArrayList<Map<String, String>>();
if(sysUserDeleteDuplicate!=null && sysUserDeleteDuplicate.size()>0){
    for (int i=0; i<sysUserDeleteDuplicate.size(); i++){
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("text", sysUserDeleteDuplicate.get(i).getRealname());
        resultMap.put("value", sysUserDeleteDuplicate.get(i).getId());
        rList.add(resultMap);
    }
}
Collections.sort(rList, new Comparator<Map<String, String>> () {
    @Override
    public int compare(Map<String, String> a1, Map<String, String> a2) {
         return a1.get("text").compareTo(a2.get("text"));
         }
        });
System.out.println(rList.toString());

output:

[{text=kate, value=1002}, {text=lucy, value=1004}, {text=peter, value=1001}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM