对象数组排序 和 类比JDK实现 sort()的方法

1.定义自己的 MyComparable 接口

1 package Test.treeSetDemo;
2 
3 public interface MyComparable <E>{
4      int compareTo(E e);
5 }

2.定义自己的 MyComparator接口

1 package Test.treeSetDemo;
2 
3 public interface MyComparator {
4      int compare(Student  student1,Student  student2);
5 }

3.定义自己的排序类 

 1 package Test.treeSetDemo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Comparator;
 5 
 6 public class MyArray {
 7     private ArrayList <Student> students = new ArrayList<>();
 8     /*
 9         1.直接排序 实现对象的排序
10         缺点:1.只能根据年龄排序
11              2.只能排一种顺序 无法先年龄后身高
12      */
13     public void sort(){
14         //使用冒泡排序比较年龄 排ArrayList中的对象
15         for (int i = 0; i < students.size(); i++) {
16             for (int j = 0; j < students.size()-1; j++) {
17                 Student current = students.get(j);
18                 Student next = students.get(j + 1);
19                 if (current.getAge()>next.getAge()){
20                     Student temp = current;
21                     students.set(j,next);
22                     students.set(j+1,temp);
23                 }
24             }
25         }
26     }
27 
28     /*
29         2.让对象可比 即对象实现comparable
30      */
31     public void sort2(){
32         //使用冒泡排序比较年龄 排ArrayList中的对象
33         for (int i = 0; i < students.size(); i++) {
34             for (int j = 0; j < students.size()-1; j++) {
35                 Student current = students.get(j);
36                 Student next = students.get(j + 1);
37                 if (current.compareTo(next)>0){
38                     Student temp = current;
39                     students.set(j,next);
40                     students.set(j+1,temp);
41                 }
42             }
43         }
44     }
45     /*
46         3.传入一个比较器对象
47     */
48     public void sort2(MyComparator comparator){
49         //使用冒泡排序比较年龄 排ArrayList中的对象
50         for (int i = 0; i < students.size(); i++) {
51             for (int j = 0; j < students.size()-1; j++) {
52                 Student current = students.get(j);
53                 Student next = students.get(j + 1);
54                 int result=comparator.compare(current,next);
55                 //要实现排序 if 这儿的boolean结果是个关键 而求boolean的这个过程不一定非要在自己类中实现
56                 if (result>0){
57                     Student temp = current;
58                     students.set(j,next);
59                     students.set(j+1,temp);
60                 }
61             }
62         }
63     }
64 
65     public ArrayList<Student> getStudents() {
66         return students;
67     }
68     public void setStudents(ArrayList<Student> students) {
69         this.students = students;
70     }
71 }

4.定义对象的类

 1 package Test.treeSetDemo;
 2 
 3 class Student implements MyComparable<Student>{
 4     private String name;
 5     private int age;
 6     private int height;
 7 
 8     public String getName() {
 9         return name;
10     }
11 
12     public void setName(String name) {
13         this.name = name;
14     }
15 
16     public int getAge() {
17         return age;
18     }
19 
20     public void setAge(int age) {
21         this.age = age;
22     }
23 
24     public Student() {
25     }
26 
27     public Student(String name, int age) {
28         this.name = name;
29         this.age = age;
30     }
31 
32     @Override
33     public int compareTo(Student o) {
34         int result=this.age -o .age;
35         return result;
36     }
37 
38     @Override
39     public String toString() {
40         return "Student{" +
41                 "name='" + name + '\'' +
42                 ", age=" + age +
43                 ", height=" + height +
44                 '}';
45     }
46 
47     public Student(String name, int age, int height) {
48         this.name = name;
49         this.age = age;
50         this.height = height;
51     }
52 
53     public int getHeight() {
54         return height;
55     }
56 
57     public void setHeight(int height) {
58         this.height = height;
59     }
60 }

5.测试

 1 package Test.treeSetDemo;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class Test {
 6     public static void main(String[] args) {
 7         MyArray myArray = new MyArray();
 8         ArrayList<Student> students = myArray.getStudents();
 9         students.add(new Student("aaa", 111,175));
10         students.add(new Student("bbb", 102,180));
11         students.add(new Student("ccc", 195,182));
12         students.add(new Student("ddd", 333,179));
13         students.add(new Student("eee", 111,180));
14         // 1.直接排序 实现对象的排序
15            myArray.sort();
16         for (Student student : students) {
17             System.out.println(student);
18         }
19         System.out.println("------------------------------------");
20         // 2.让对象可比 即对象实现comparable
21         myArray.sort2();
22         for (Student student : students) {
23             System.out.println(student);
24         }
25         System.out.println("------------------------------------");
26         //3.传入一个比较器对象
27         myArray.sort2(new MyComparator() {
28             @Override
29             public int compare(Student student1, Student student2) {
30                 int result=student1.getHeight()-student2.getHeight();//先比身高 升序
31                 result= result==0?student2.getAge()-student1.getAge():result;//后比年龄 降序
32                 return result;
33             }
34         });
35         for (Student student : students) {
36             System.out.println(student);
37         }
38     }
39 }
posted @ 2022-06-24 08:59  ACMAN-Mr.Lee  阅读(32)  评论(0编辑  收藏  举报