[英]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.