对List集合嵌套了map集合对double值进行排序

时间:2022-07-19 20:28:57

/*[
{
"repairo": "asda",
"num": 88.71
},
{
"repairo": "fghfghfgh",
"num": 50
},
{
"repairo": "ytutyu",
"num": 5.47
},
{
"repairo": "qweqe",
"num": 4.17
}
]
*/

if (lists.size() > 0) {
Collections.sort(lists,new Comparator<Map>() {
@Override
public int compare(Map o1, Map o2) {

//比较两个对象的顺序,如果前者小于、等于或者大于后者,则分别返回-1/0/1

double t1=(double) o1.get("num");
double t2=(double) o2.get("num");
return t1<t2?1:(t1>t2)?-1:1;
}
});
}