Comparable是在集合内部定义方法的排序,位于java.lang下。
Comparator是在集合外部实现的排序,位于java.util下。
Comparable 实现案例:
public class DeptBean implements Comparable<DeptBean>{ private int deptId; private String departmentName; private int parentDepartmentId; private int orderIndex; public DeptBean() { super(); } public DeptBean(int deptId, String departmentName, int orderIndex) { super(); this.deptId = deptId; this.departmentName = departmentName; this.orderIndex = orderIndex; } @Override public String toString() { return "DeptBean [deptId=" + deptId + ", departmentName=" + departmentName + ", parentDepartmentId=" + parentDepartmentId + ", orderIndex=" + orderIndex + ", childDept=" + childDept + ", userList=" + userList + "]"; } @Override public int compareTo(DeptBean o) { if(this.orderIndex < o.orderIndex){ return -1; }else{ return 1; } } public static void main(String[] args) { List<DeptBean> list = new ArrayList<DeptBean>(); list.add(new DeptBean(1,"部门1",2)); list.add(new DeptBean(2,"部门2",12)); list.add(new DeptBean(3,"部门3",5)); list.add(new DeptBean(4,"部门4",4)); list.add(new DeptBean(5,"部门5",8)); list.add(new DeptBean(6,"部门6",1)); for(DeptBean dept : list){ System.out.println(dept.getDepartmentName()+" "+dept.getOrderIndex()); } Collections.sort(list); System.out.println("排序后"); for(DeptBean dept : list){ System.out.println(dept.getDepartmentName()+" "+dept.getOrderIndex()); } } }
Comparator 实现案例:
public class DeptBean{ private int deptId; String departmentName; int orderIndex; public DeptBean() { super(); } public DeptBean(int deptId, String departmentName, int orderIndex) { super(); this.deptId = deptId; this.departmentName = departmentName; this.orderIndex = orderIndex; } public static void main(String[] args) { List<DeptBean> list = new ArrayList<DeptBean>(); list.add(new DeptBean(1,"部门1",2)); list.add(new DeptBean(2,"部门2",12)); list.add(new DeptBean(3,"部门3",5)); list.add(new DeptBean(4,"部门4",4)); list.add(new DeptBean(5,"部门5",8)); list.add(new DeptBean(6,"部门6",1)); for(DeptBean dept : list){ System.out.println(dept.departmentName+" "+dept.orderIndex); } MyComparator comparator = new MyComparator(); Collections.sort(list,comparator); System.out.println("排序后"); for(DeptBean dept : list){ System.out.println(dept.departmentName+" "+dept.orderIndex); } } } public class MyComparator implements Comparator<DeptBean>{ public int compare(DeptBean o1, DeptBean o2) { if(o1.orderIndex < o2.orderIndex){ return -1; }else{ return 1; } } } // 或者另一种写法 public static void main(String[] args) { List<DeptBean> list = new ArrayList<DeptBean>(); list.add(new DeptBean(1,"部门1",2)); list.add(new DeptBean(2,"部门2",12)); list.add(new DeptBean(3,"部门3",5)); list.add(new DeptBean(4,"部门4",4)); list.add(new DeptBean(5,"部门5",8)); list.add(new DeptBean(6,"部门6",1)); for(DeptBean dept : list){ System.out.println(dept.departmentName+" "+dept.orderIndex); } Collections.sort(list,new Comparator<DeptBean>(){ @Override public int compare(DeptBean o1, DeptBean o2) { if(o1.orderIndex > o2.orderIndex){ //如果o1.orderIndex > o2.orderIndex,返回正数,o1排在o2的后面,否则排在前面 return 1; }else{ return -1; } } }); System.out.println("排序后"); for(DeptBean dept : list){ System.out.println(dept.departmentName+" "+dept.orderIndex); } }
如果map的集合需要根据值来排序,需要将map转成list,然后排序
List<Map.Entry<Integer, DeptBean>> list = new ArrayList<Map.Entry<Integer, DeptBean>>(map1.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Integer, DeptBean>>(){ public int compare(Entry<Integer, DeptBean> o1,Entry<Integer, DeptBean> o2) { if(o1.getValue().getOrderIndex() < o2.getValue().getOrderIndex()){ return 1; }else if(o1.getValue().getOrderIndex() < o2.getValue().getOrderIndex()){ return -1; }else{ return 0; } } }); System.out.println("排序后"); for(Map.Entry<Integer, DeptBean> mapping : list){ System.out.println(mapping.getKey()+" "+ mapping.getValue().getDepartmentName()+" "+mapping.getValue().getOrderIndex()); }
参考:https://blog.csdn.net/tolcf/article/details/52229068