类customer
public class Customer { public String name; public int age; Customer(String name, int age){ this.name = name; this.age = age; } 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; } }
排序
public class main { public static void main(String[] args) { List<Customer> customers = new ArrayList<Customer>(); List<Customer> customers2 = new ArrayList<Customer>(); customers.add(new Customer("拉克丝", 12)); customers.add(new Customer("团藏", 12)); customers.add(new Customer("布拉格", 15)); customers.add(new Customer("尼古拉斯赵四", 12)); customers.add(new Customer("狗娃", 12)); customers.add(new Customer("阿里郎", 15)); customers.add(new Customer("雅蠛蝶", 15)); for (Customer customer:customers) { customers2.add(customer); Collections.sort(customers2, new Comparator<Customer>(){ @Override public int compare(Customer o1, Customer o2){ return Collator.getInstance(Locale.CHINESE).compare(o1.getName(), o2.getName()); } }); } //打印 for (Customer customer:customers2) { System.out.println(customer.getName()); } } }
打印结果
阿里郎
布拉格
狗娃
拉克丝
尼古拉斯赵四
团藏
雅蠛蝶
原文:https://www.cnblogs.com/Jomini/p/10252964.html