1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package com.itheima.demo05.Collections;
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator;
public class Demo03Sort { public static void main(String[] args) { ArrayList<Integer> list01 = new ArrayList<>(); list01.add(1); list01.add(3); list01.add(2); System.out.println(list01);
Collections.sort(list01, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; } });
System.out.println(list01);
ArrayList<Student> list02 = new ArrayList<>(); list02.add(new Student("a迪丽热巴",18)); list02.add(new Student("古力娜扎",20)); list02.add(new Student("杨幂",17)); list02.add(new Student("b杨幂",18)); System.out.println(list02);
Collections.sort(list02, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { int result = o1.getAge()-o2.getAge(); if(result==0){ result = o1.getName().charAt(0)-o2.getName().charAt(0); } return result; }
});
System.out.println(list02); } }
|