按照ArrayList指定的某一列进行排序
按照ArrayList中指定的某一列进行排序
private void sortArrayList(ArrayList nameAndWeight,final int index) { // Collections.sort(nameAndWeight, new Comparator() { @Override public int compare(Object o1, Object o2) { ArrayList list1 = (ArrayList) o1; ArrayList list2 = (ArrayList) o2; int compare =0 ; if(((Double) list1.get(index) - (Double) list2 .get(index))>0){ compare = 1; }else if(((Double) list1.get(index) - (Double) list2 .get(index))<0){ compare = -1; } return compare; } }); }
新写一个比较器Comparator(Object o1,Object o2);