如何实现按照List集合中对象某个属性排序?
工作中遇到按照类的某个属性排列,这个属性是个枚举类型,按照要求的优先级排列。
可以使用ComparatorChain、BeanComparator、FixedOrderComparator实现。
举一个例子:
学生类:包含姓名、年龄、性别、性格。性格是个枚举类型。
性格类:枚举类型,分为四类:内向,外向,太监,不知道四种类型。
SortUtil类:排序类。
Student.java
- import java.util.ArrayList;
- import java.util.List;
- public class Student {
- private String name;
- private int age;
- private boolean sex;
- private Character character;
- public Student(String name, int age, boolean sex, Character character){
- this.name = name;
- this.age = age;
- this.sex = sex;
- this.character = character;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public void setSex(boolean sex) {
- this.sex = sex;
- }
- public boolean getSex() {
- return sex;
- }
- public void setCharacter(Character character) {
- this.character = character;
- }
- public Character getCharacter() {
- return character;
- }
- @Override
- public String toString() {
- String sexString = sex == false ? "male" : "female";
- return "名字:" + name + " 年龄: " + age + " 性别: " + sexString + " 性格: " + character;
- }
- public static void main(String args[]) {
- Student student1 = new Student("spring", 20, false, Character.IN);
- Student student2 = new Student("summer", 18, false, Character.OUT );
- Student student3 = new Student("automn", 25, false, Character.UNKNOWN );
- Student student4 = new Student("winter", 25, true, Character.UNKNOWN);
- List<Student> list1 = new ArrayList<Student>();
- list1.add(student1);
- list1.add(student3);
- list1.add(student2);
- list1.add(student4);
- System.out.println("没有排序之前:");
- for(Student student : list1) {
- System.out.println(student);
- }
- SortUtil.sortByWhat(list1);
- System.out.println("排序之后:");
- for(Student student : list1) {
- System.out.println(student);
- }
- }
- }
Character类:
- public enum Character {
- IN("内向"),
- OUT("外向"),
- BOTH("太监"),
- UNKNOWN("不知道");
- private String name;
- private Character(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public String toString() {
- return name;
- }
- }
SortUtil类:
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import org.apache.commons.beanutils.BeanComparator;
- import org.apache.commons.collections.comparators.ComparatorChain;
- import org.apache.commons.collections.comparators.FixedOrderComparator;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class SortUtil {
- private static final Log Logger = LogFactory.getLog(SortUtil.class);
- @SuppressWarnings("unchecked")
- public static void sortByWhat(List<Student> list){
- try{
- Character[] CharacterType = {Character.UNKNOWN,Character.IN ,Character.OUT,Character.BOTH};
- Comparator TypeComparator = new FixedOrderComparator(CharacterType);
- ComparatorChain moInfoComparator = new ComparatorChain();
- moInfoComparator.addComparator(new BeanComparator("character",TypeComparator));
- moInfoComparator.addComparator(new BeanComparator("age"),true);
- moInfoComparator.addComparator(new BeanComparator("name"));
- moInfoComparator.addComparator(new BeanComparator("sex"));
- Collections.sort(list, moInfoComparator);
- } catch (Exception e) {
- Logger.error(e.getMessage());
- }
- }
- }
- moInfoComparator.addComparator(new BeanComparator("character",TypeComparator));
这个类中首先按照性格属性来排序,利用FixedOrderComparator设置优先级,性格属性Character.UNKOWN排第一,
然后IN,其次OUT,最后BOTH。如果性格属性相同,按照age排序,true表示逆序(默认排序是从小到大),然后按
照name,最后按照sex属性排序。
需要导入commons-beanutils,commons-logging,commons-collections包。
结果:
可以看到排序之后首先按照性格排序,性格是枚举类型,排序规则用FixedOrderComparator定义,性格相同按照年龄,所以:
- <span style="white-space:pre"> </span>moInfoComparator.addComparator(new BeanComparator("character",TypeComparator));
- moInfoComparator.addComparator(new BeanComparator("age"),true);
- moInfoComparator.addComparator(new BeanComparator("name"));
- moInfoComparator.addComparator(new BeanComparator("sex"));
这四句话的顺序决定了先已哪个属性排序。