Map KEY 排序

    List<String> snList = new ArrayList<>();
    List<Position> positionList = VehicleControlUtil.getVehicleLocationBaiDuFromSn(strSn);
    StringBuffer sb2 = new StringBuffer();
    for(Position position : positionList)
    {
     sb2.append(position.getY()+","+position.getX()+"|");
     snList.add(position.getSn());
    }
    String destination = sb2.toString().substring(0, sb2.toString().length() - 1);
    Map<String, Double> mapSNandDistance = new HashMap<String, Double>();
    List<Map> resutlMap = BaiDuAPIUtil.routematrix(latitude+","+longitude, destination);
    int iSort =0;
    for(Map  map:resutlMap){//先取出所有车辆的距离,然后在进行取出最近的6辆车
      Map mapD = (Map) map.get("distance");    
      mapSNandDistance.put(snList.get(iSort).toString(), Double.parseDouble(mapD.get("value").toString()));
      iSort++;
    }
    
    List<Map.Entry<String, Double>> infoIds = new ArrayList<Map.Entry<String, Double>>(mapSNandDistance.entrySet());
    Collections.sort(infoIds, new Comparator<Map.Entry<String, Double>>() {  
        public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {     
            return o2.getValue() - o1.getValue() > 0 ? -1 : 1;
        }
    });
    infoIds = infoIds.subList(0, 6);
    snList = new ArrayList<>();

原文地址:https://www.cnblogs.com/cuijinlong/p/6743596.html