1. 对于普通类型(Stirng、Integer)升序:Collections.sort(arrayList),降序:Collections.reverse(arrayList)
public void testSortData(){
ArrayList<String> arrayList = new ArrayList<String>(5);
arrayList.add("AAAA");
arrayList.add("CCCC");
arrayList.add("BBBB");
arrayList.add("EEEE");
arrayList.add("DDDD");
Collections.sort(arrayList); //升序
//Collections.reverse(arrayList); //降序
for (String s : arrayList) {
System.out.println(s);
}
}
2. 对于复杂类型(实体类)
(1)Collections.sort 升序: o1.getEmail().compareTo(o2.getEmail()),降序:o2.getEmail().compareTo(o1.getEmail())
public void testSortObject() {
List<Customer> list = new ArrayList<Customer>();
list.add(new Customer(5, "张三", "WN001", 88));
list.add(new Customer(8, "李四", "N002", 75));
list.add(new Customer(9, "王五", "KN003", 99));
list.add(new Customer(10, "赵六", "UN004", 58));
list.add(new Customer(11, "田七", "LN005", 67));
list.add(new Customer(12, "小八", "ON006", 58));
list.add(new Customer(6, "唐七", "TN007", 35));
list.add(new Customer(7, "商十", "SN008", 78));
//使用Collections.sort排序
Collections.sort(list, new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
return o1.getEmail().compareTo(o2.getEmail()); //升序
//return o2.getEmail().compareTo(o1.getEmail()); //降序
}
});
for (Customer s : list) {
System.out.println(s.getEmail());
}
}
中文排序
public void testSortObject() {
List<Customer> list = new ArrayList<Customer>();
list.add(new Customer(5, "张三", "WN001", 88));
list.add(new Customer(8, "李四", "N002", 75));
list.add(new Customer(9, "王五", "KN003", 99));
list.add(new Customer(10, "赵六", "UN004", 58));
list.add(new Customer(11, "田七", "LN005", 67));
list.add(new Customer(12, "小八", "ON006", 58));
list.add(new Customer(6, "唐七", "TN007", 35));
list.add(new Customer(7, "商十", "SN008", 78));
Collections.sort(list, new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
Collator cmp = Collator.getInstance(java.util.Locale.CHINA);// 中文首字母排序规则
//if (cmp.compare(o1.getLastName(), o2.getLastName())>0){ //升序
if (cmp.compare(o2.getLastName(), o1.getLastName()) > 0) { //降序
return 1;
} else {
return -1;
}
}
});
for (Customer s : list) {
System.out.println(s.getLastName());
}
}
(2)Stream().sort 升序:Comparator.comparing(Customer::getEmail), 降序:(Customer::getEmail).reversed()
public void testStreamSortObject() {
List<Customer> list = new ArrayList<Customer>();
list.add(new Customer(5, "张三", "WN001", 88));
list.add(new Customer(8, "李四", "N002", 75));
list.add(new Customer(9, "王五", "KN003", 99));
list.add(new Customer(10, "赵六", "UN004", 58));
list.add(new Customer(11, "田七", "LN005", 67));
list.add(new Customer(12, "小八", "ON006", 58));
list.add(new Customer(6, "唐七", "TN007", 35));
list.add(new Customer(7, "商十", "SN008", 78));
//List<Customer> customerList = list.stream().sorted(Comparator.comparing(Customer::getEmail)).collect(Collectors.toList());//升序
List<Customer> customerList = list.stream().sorted(Comparator.comparing(Customer::getEmail).reversed()).collect(Collectors.toList());//降序
for (Customer s : customerList) {
System.out.println(s.getEmail());
}
}