在使用List集合时,通常情况下,希望从集合中得到的对象是按照一定顺序排列的,但是List集合的默认排序方式为按照对象的插入顺序来排序的。但是可以通过 java.util.Collections类的静态方法sort(List<T> list)、sort(List<T> list, Comparator<? super T> c)或reverse(List<?> list)对集合中的对象进行客户化排序。那么接下来就看看sort排序的两种具体实现。
方法一:实现java.lang.Comparable接口
在创建实体类UserA时就实现java.lang.Comparable接口,然后在该接口方法中自定义排序原则,如下:
package com.pes_soft.ppms.sort; import java.io.Serializable; @SuppressWarnings("serial") public class UserA implements Serializable, Comparable<UserA> { private String userId; private String name; private int age; public UserA() {} public UserA(String userId, String name, int age) { this.userId = userId; this.name = name; this.age = age; } /** * 排序比较方法 */ @Override public int compareTo(UserA user) { // 首先比较年龄(age),如果年龄相同,则比较名字(userId) int flag = String.valueOf(this.age).compareTo(String.valueOf(user.getAge())); if (flag == 0) { return this.userId.compareTo(user.getUserId()); } else { return flag; } } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
方法二:实现java.util.Comparator接口
1、创建实体类UserB,不实现任何接口:
package com.pes_soft.ppms.sort; public class UserB { private String userId; private String name; private int age; public UserB() {} public UserB(String userId, String name, int age) { this.userId = userId; this.name = name; this.age = age; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
2、创建比较器类,实现java.util.Comparator接口,然后在该接口方法中自定义排序原则,如下:
package com.pes_soft.ppms.sort; import java.util.Comparator; public class UserBComparator implements Comparator<UserB> { /** * 排序比较方法 */ @Override public int compare(UserB user1, UserB user2) { //首先比较年龄(age),如果年龄相同,则比较名字(userId) int flag = String.valueOf(user1.getAge()).compareTo(String.valueOf(user2.getAge())); if (flag == 0) { return user1.getUserId().compareTo(user2.getUserId()); }else { return flag; } } }
测试类:
package com.pes_soft.ppms.sort; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortTest { public static void main(String[] args) { List<UserA> userlistA = new ArrayList<UserA>(); List<UserB> userlistB = new ArrayList<UserB>(); userlistA.add(new UserA("ff","诸葛亮",5)); userlistA.add(new UserA("dd","张三",4)); userlistA.add(new UserA("aa","刘德华",1)); userlistA.add(new UserA("ee","刘表",5)); userlistA.add(new UserA("bb","曹操",2)); userlistA.add(new UserA("cc","深田恭子",3)); userlistA.add(new UserA("gg","SAP",6)); userlistB.add(new UserB("ff","诸葛亮",5)); userlistB.add(new UserB("dd","张三",4)); userlistB.add(new UserB("aa","刘德华",1)); userlistB.add(new UserB("ee","刘表",5)); userlistB.add(new UserB("bb","曹操",2)); userlistB.add(new UserB("cc","深田恭子",3)); userlistB.add(new UserB("gg","SAP",6)); Collections.sort(userlistA); UserBComparator userBComparator = new UserBComparator(); Collections.sort(userlistB, userBComparator); for (UserA user : userlistA) { System.out.println(user.getUserId() + " | " + user.getAge() + " | " + user.getName()); } System.out.println("=======分隔线======="); for (UserB user : userlistB) { System.out.println(user.getUserId() + " | " + user.getAge() + " | " + user.getName()); } } }
测试结果,两种排序方式的排序结果是一样的:
aa | 1 | 刘德华 bb | 2 | 曹操 cc | 3 | 深田恭子 dd | 4 | 张三 ee | 5 | 刘表 ff | 5 | 诸葛亮 gg | 6 | SAP =======分隔线======= aa | 1 | 刘德华 bb | 2 | 曹操 cc | 3 | 深田恭子 dd | 4 | 张三 ee | 5 | 刘表 ff | 5 | 诸葛亮 gg | 6 | SAP