- Collections.sort(集合)排序
/*直接按值的大小排序*/
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
nums.add(100);
nums.add(8);
nums.add(6);
nums.add(10);
System.out.println("排序前:"+nums);
Collections.sort(nums);
System.out.println("排序后:"+nums);
}
输出为:
排序前:[100, 8, 6, 10]
排序后:[6, 8, 10, 100]
- List<Map<String, Object>>某个key值的大小排序
public static void main(String[] args) {
List<Map<String, Object>> arrayList2 = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
map1.put("aa", -1.1);
map2.put("aa", 2.2);
map3.put("aa", -3.2);
map1.put("bb", -10);
map2.put("bb", 2);
map3.put("bb", -6.6);
arrayList2.add(map1);
arrayList2.add(map2);
arrayList2.add(map3);
sortTableDataByKeyName(arrayList2,"bb");
for(int a = 0 ; a<arrayList2.size();a++) {
System.out.println("aa:"+arrayList2.get(a).get("aa")+" bb:"+arrayList2.get(a).get("bb"));
}
}
public static void sortTableDataByKeyName(List<Map<String,Object>> marketUnitList,final String KeyName){
Collections.sort(marketUnitList,new Comparator<Map<String,Object>>(){
@Override
public int compare(Map<String, Object> o1,Map<String, Object> o2) {
if(!"".equals(