zoukankan      html  css  js  c++  java
  • List<Map<String, Object>> 中根据某一个属性进行排序

    //key 是要排序的属性,low1是要list排序的起始位置,high1是list的终止位置
    private static List<Map<String, Object>> sortList(List<Map<String, Object>> list,String key, int low1, int high1){
    int low = low1;
    int high = high1;
    String value = list.get(low).get(key).toString();
    value = value.substring(0, value.length() - 1);
    double x = Double.valueOf(value);
    Map<String,Object> temp = list.get(low);
    if(low1<high1){
    while(low<high){
    while(low<high && Double.valueOf(list.get(high).get(key).toString().substring(0, list.get(high).get(key).toString().length() -1))<=x){
    high--;
    }
    list.set(low, list.get(high));
    while(low < high && Double.valueOf(list.get(low).get(key).toString().substring(0, list.get(low).get(key).toString().length()-1))>=x){
    low++;
    }
    list.set(high, list.get(low));
    }
    list.set(low, temp);
    if(low>low1) sortList(list,key,low1,low-1);
    if(high<high1) sortList(list,key,low+1,high1);
    }
    return list;
    }

    ps:本例子中list要排序的属性名字:"AnjianSera" ,属性值带有%的,所以要截取String,再转化成Double类型进行比较;

          程序中是倒叙排序(desc),如果要想是正序排序(asc),

    while(low<high && Double.valueOf(list.get(high).get(key).toString().substring(0, list.get(high).get(key).toString().length() -1))<=x)

    while(low < high && Double.valueOf(list.get(low).get(key).toString().substring(0, list.get(low).get(key).toString().length()-1))>=x)

    两句代码的最后的<= 和 >= ,修改成>=和<= 即可

  • 相关阅读:
    C++ String详解
    乏力的编码很累,这里有私货..
    给自己~~微语&&歌单
    2019CSP-J第二轮 B题C题
    HDU 3966 树链剖分+树状数组 模板
    HDU 2255 KM算法 二分图最大权值匹配
    HDU 4280 ISAP+BFS 最大流 模板
    HDU 6181 第k短路
    假装会python--爬取贴吧正文
    HDU 6170 dp
  • 原文地址:https://www.cnblogs.com/fuqiang-terry/p/6872396.html
Copyright © 2011-2022 走看看