//测试参考demo
List<Map<String , Integer>>
public static void main(String[] args) {
cancelOrder();
}
public static List<Map<String , Integer>> cancelOrder(){
int sourcePorf = 1;//目标点
int length =1000;
/*int [] indexOfVehicle =new int[] {123,126,138,145,235,564};//数组小车*/
List<Map<String , Integer>> indexOfVehicle =new ArrayList<>();
Map<String , Integer> params1= new HashMap<>();
params1.put("distance",123);//位置
params1.put("id",1);//车子序号
indexOfVehicle.add(params1);
Map<String , Integer> params2= new HashMap<>();
params2.put("distance",126);
params2.put("id",2);
indexOfVehicle.add(params2);
Map<String , Integer> params3= new HashMap<>();
params3.put("distance",138);
params3.put("id",3);
indexOfVehicle.add(params3);
Map<String , Integer> params4= new HashMap<>();
params4.put("distance",145);
params4.put("id",4);
indexOfVehicle.add(params4);
Map<String , Integer> params5= new HashMap<>();
params5.put("distance",235);
params5.put("id",5);
indexOfVehicle.add(params5);
Map<String , Integer> params6= new HashMap<>();
params6.put("distance",564);
params6.put("id",6);
indexOfVehicle.add(params6);
for(Map<String , Integer> m : indexOfVehicle) {
int a = 0;
if(Integer.parseInt(m.get("distance").toString()) < length){
a = Math.abs(sourcePorf - Integer.parseInt(m.get("distance").toString()));//绝对值
m.put("distance",a);
}
}
Collections.sort(indexOfVehicle, new Comparator<Map<String, Integer>>() {
@Override
public int compare(Map<String, Integer> o1, Map<String, Integer> o2) {
Integer index1 = o1.get("distance");//1是从你list里面拿出来的一个
Integer index2 = o1.get("distance") ; //2是从你list里面拿出来的第二个
return index1 - index2;//比较大小 // 两个互换一个是倒叙 一个是 顺序
}
});
System.out.println(indexOfVehicle);
// [{distance=122, id=1}, {distance=125, id=2}, {distance=137, id=3}, {distance=144, id=4}, {distance=234, id=5}, {distance=563, id=6}]
return indexOfVehicle;
}