import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.comparators.ComparatorChain; import com.google.gson.Gson; public class Main { public static class Cats{ private Integer years; private String name; private String entryDate; public Integer getYears() { return years; } public void setYears(Integer years) { this.years = years; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEntryDate() { return entryDate; } public void setEntryDate(String entryDate) { this.entryDate = entryDate; } } @SuppressWarnings("unchecked") public static void main(String[] args) { List<Cats> cats = new ArrayList<>(); Cats cat = new Cats(); cat.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss")); cat.setName("smith"); cat.setYears(21); Cats cat1 = new Cats(); cat1.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss")); cat1.setName("KAMIER"); cat1.setYears(36); Cats cat2 = new Cats(); cat2.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss")); cat2.setName("STIWEN"); cat2.setYears(18); Cats cat3 = new Cats(); cat3.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss")); cat3.setName("AKAI"); cat3.setYears(88); cats.add(cat3);cats.add(cat2);cats.add(cat1);cats.add(cat); ComparatorChain comparatorChain = new ComparatorChain(); comparatorChain.addComparator(new BeanComparator("entryDate")); //comparatorChain.addComparator(new BeanComparator("years")); // Arrays.sort(cats.toArray(), comparatorChain); Collections.sort(cats, comparatorChain); System.out.println("多字段排序:"); System.out.println(new Gson().toJson(cats)); } }