分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由大到小排序,
年龄一样,按名字第一个字母排序,字符大的在前面,同理如果第一个字母一样比较第二个,不行再比较最后一个字母。
姓名(String) |
年龄(int) |
分数(float) |
apo |
20 |
90.0F |
bpo |
22 |
90.0F |
cpo |
20 |
99.0F |
dpo |
22 |
100.0F |
ean |
23 | 88.0F |
lan |
23 |
88.0F |
laj |
23 |
88.0F |
lvc |
23 |
88.0F |
|
编写一个Student类用来实现Comparable<Student>接口,并在其中重写CompareTo(Student o)方法
1 package demo13_18; 2 3 public class Student implements Comparable<Student>{ 4 5 private String name; 6 private int age; 7 private float score; 8 9 public Student() { 10 } 11 12 public Student(String name, int age, float score) { 13 this.name = name; 14 this.age = age; 15 this.score = score; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 } 33 34 public float getSorce() { 35 return score; 36 } 37 38 @Override 39 public String toString() { 40 return "Student{" + 41 "name='" + name + '\'' + 42 ", age=" + age + 43 ", score=" + score + 44 '}'; 45 } 46 47 public void setSorce(float score) { 48 this.score = score; 49 } 50 51 @Override 52 public int compareTo(Student o) { 53 //定义一个中间变量判断成绩的大小 如果成绩相等 比较年龄 54 int result = (int)(o.getSorce() - this.getSorce()); 55 if(result==0){//比较年龄大的在前面 56 result = o.getAge()-this.getAge(); 57 if(result==0){//比较姓名第一个字母,大的在前面 58 result = o.getName().charAt(0)-this.getName().charAt(0); 59 if(result==0){//比较姓名第二个字母,大的在前面 60 result = o.getName().charAt(1)-this.getName().charAt(1); 61 if(result==0){//比较姓名最后一个字母,大的在前面 62 result = o.getName().charAt(o.getName().length()-1)-this.getName().charAt(this.getName().length()-1); 63 } 64 } 65 } 66 } 67 return result; 68 69 } 70 71 }
用Comparable 对ArrayList进行排序
1 @Test 2 public void fun01(){ 3 Student stu1 = new Student("apo",20,90.0f); 4 Student stu2 = new Student("bpo",22,90.0f); 5 Student stu3 = new Student("cpo",20,90.0f); 6 Student stu4 = new Student("dpo",20,100.0f); 7 Student stu5 = new Student("ean",23,88.0f); 8 Student stu6 = new Student("lan",23,88.0f); 9 Student stu7 = new Student("laj",23,88.0f); 10 Student stu8 = new Student("lvc",23,88.0f); 11 12 List<Student > arr = new ArrayList<>(); 13 arr.add(stu1); 14 arr.add(stu2); 15 arr.add(stu3); 16 arr.add(stu4); 17 arr.add(stu5); 18 arr.add(stu6); 19 arr.add(stu7); 20 arr.add(stu8); 21 Collections.sort(arr); 22 System.out.println("降序排序"); 23 for(int i=0;i<arr.size();i++){ 24 System.out.println("分数:"+arr.get(i).getSorce()+" 年龄: "+arr.get(i).getAge()+" 姓名: "+arr.get(i).getName()); 25 } 26 System.out.println("升序排序"); 27 for(int i=arr.size()-1;i>=0;i--){ 28 System.out.println("分数:"+arr.get(i).getSorce()+" 年龄: "+arr.get(i).getAge()+" 姓名: "+arr.get(i).getName()); 29 } 30 }
结果:注意看姓名的排序
降序排序
分数:100.0 年龄: 20 姓名: dpo 分数:90.0 年龄: 22 姓名: bpo 分数:90.0 年龄: 20 姓名: cpo 分数:90.0 年龄: 20 姓名: apo 分数:88.0 年龄: 23 姓名: lvc 分数:88.0 年龄: 23 姓名: lan 分数:88.0 年龄: 23 姓名: laj 分数:88.0 年龄: 23 姓名: ean
升序排序
分数:88.0 年龄: 23 姓名: ean 分数:88.0 年龄: 23 姓名: laj 分数:88.0 年龄: 23 姓名: lan 分数:88.0 年龄: 23 姓名: lvc 分数:90.0 年龄: 20 姓名: apo 分数:90.0 年龄: 20 姓名: cpo 分数:90.0 年龄: 22 姓名: bpo 分数:100.0 年龄: 20 姓名: dpo
当然除了Comparable 也可以用Comparetor对ArrayList进行排序
1 @Test 2 public void fun02(){ 3 Student stu1 = new Student("apo",20,90.0f); 4 Student stu2 = new Student("bpo",22,90.0f); 5 Student stu3 = new Student("cpo",20,90.0f); 6 Student stu4 = new Student("dpo",20,100.0f); 7 Student stu5 = new Student("ean",23,88.0f); 8 Student stu6 = new Student("lan",23,88.0f); 9 Student stu7 = new Student("laj",23,88.0f); 10 Student stu8 = new Student("lvc",23,88.0f); 11 12 List<Student > arr = new ArrayList<>(); 13 arr.add(stu1); 14 arr.add(stu2); 15 arr.add(stu3); 16 arr.add(stu4); 17 arr.add(stu5); 18 arr.add(stu6); 19 arr.add(stu7); 20 arr.add(stu8); 21 //写一个Compelator方法 22 Collections.sort(arr, new Comparator<Student>() { 23 @Override 24 public int compare(Student o1, Student o2) { 25 //定义一个中间变量判断成绩的大小 如果成绩相等 比较年龄 26 int result = (int)(o2.getSorce() - o1.getSorce()); 27 if(result==0){//比较年龄大的在前面 28 result = o2.getAge()-o1.getAge(); 29 if(result==0){//比较姓名第一个字母,大的在前面 30 result = o2.getName().charAt(0)-o1.getName().charAt(0); 31 if(result==0){//比较姓名第二个字母,大的在前面 32 result = o2.getName().charAt(1)-o1.getName().charAt(1); 33 if(result==0){//比较姓名最后一个字母,大的在前面 34 result = o2.getName().charAt(o2.getName().length()-1)-o1.getName().charAt(o1.getName().length()-1); 35 } 36 } 37 } 38 } 39 return result; 40 41 } 42 }); 43 System.out.println("降序排序"); 44 for(int i=0;i<arr.size();i++){ 45 System.out.println("分数:"+arr.get(i).getSorce()+" 年龄: "+arr.get(i).getAge()+" 姓名: "+arr.get(i).getName()); 46 } 47 System.out.println("升序排序"); 48 for(int i=arr.size()-1;i>=0;i--){ 49 System.out.println("分数:"+arr.get(i).getSorce()+" 年龄: "+arr.get(i).getAge()+" 姓名: "+arr.get(i).getName()); 50 } 51 }
结果也是一样的
降序排序
分数:100.0 年龄: 20 姓名: dpo 分数:90.0 年龄: 22 姓名: bpo 分数:90.0 年龄: 20 姓名: cpo 分数:90.0 年龄: 20 姓名: apo 分数:88.0 年龄: 23 姓名: lvc 分数:88.0 年龄: 23 姓名: lan 分数:88.0 年龄: 23 姓名: laj 分数:88.0 年龄: 23 姓名: ean
升序排序
分数:88.0 年龄: 23 姓名: ean 分数:88.0 年龄: 23 姓名: laj 分数:88.0 年龄: 23 姓名: lan 分数:88.0 年龄: 23 姓名: lvc 分数:90.0 年龄: 20 姓名: apo 分数:90.0 年龄: 20 姓名: cpo 分数:90.0 年龄: 22 姓名: bpo 分数:100.0 年龄: 20 姓名: dpo