简体   繁体   English

如何排序 java 列表<map<string, object> &gt; 按字符串升序? </map<string,>

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

My java code is this:我的 java 代码是这样的:

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.我想按String升序对rList中的元素进行排序。 And the data in rList likes this: rList 中的数据是这样的:

[{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.由于每个列表元素都是Map可能包含多个Object实例,因此您首先需要重新定义排序标准。

It would work if you say that you want to sort the list by the greatest Object in the Map .如果您说要按Object中最大的Map对列表进行排序,它将起作用。

But as java.lang.Object does not define java.lang.Comparable<Object> , there is no natural order for instances of Object . 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.因此,您还需要为Object定义排序标准。 As there are not that many attributes, only the hash code or the result of Object.toString() would work.由于没有那么多属性,只有 hash 代码或Object.toString()的结果可以工作。

With these informations, you can implement a Comparator that can be used as argument to List.sort() .使用这些信息,您可以实现一个Comparator ,它可以用作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正如 Shubham 和 tquadrat 所说,我将List<Map<String, Object>>修改为
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: output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对列表进行排序<Map<String,Object> &gt; 在 Java 中 - How to i sort a List<Map<String,Object>> in java 如何对 Map 类型的地图进行排序<String, List<String> &gt; 在 Java 中 - How to sort a map of type Map<String, List<String>> in Java 如何将Map <String,string>添加到java中的Map <String,List <Object >>中 - How to add Map<String, string> into Map<String, List<Object>> in java 如何迭代 Map <String, List<Map<String, Object> &gt;&gt; 在 Java 中? - how to iterate Map<String, List<Map<String, Object>>> in java? 如何分组清单 <Map<String,Object> &gt;到地图 <String,List<Map<String,Object> &gt;在Java8中 - How to group List<Map<String,Object>> to Map<String,List<Map<String,Object>> in Java8 如何迭代列表<Map<String,Object> &gt; 在 Java 中 - How to iterate List<Map<String,Object>> in java 如何转换清单 <Object> 进入清单 <Map<String,String> &gt;在Java8中 - How to convert List<Object> into List<Map<String,String>> in java8 如何按升序对包含列表的对象进行排序 - How to sort the Object containing list in asc order android-如何排序列表 <Map<String, String> &gt; - android - How to sort List<Map<String, String>> 比较列表<map<string, string> &gt; 到列表<map<string, object> &gt; Java </map<string,></map<string,> - Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM