list的排序问题
一,list集合的泛型是int
二,泛型为自定义类
需要将自定义的类实现Comparable类,然后重写compareTo方法,在此方法里实现比较规则
输出测试:
三,自定义类,方法二
自定义方法类:
排序输出测试:使用collections.sort(),连个参数的
四,直接使用上述方法,使用匿名内部类
Collections.sort(list, new Comparator<HashMap>() { @Override public int compare(HashMap o1, HashMap o2) { String code1 = (String) o1.get("code"); String code2 = (String) o2.get("code"); int c1 = 0; int c2 = 0; try { c1 = Integer.parseInt(code1); } catch (Exception e) { return -1; } try { c2 = Integer.parseInt(code2); } catch (Exception e) { return 1; } if (c1 > c2) { return 1; } else if (c1 < c2) { return -1; } else { return 0; } } });