当前位置:网站首页>Comparator (for arrays.sort)

Comparator (for arrays.sort)

2022-06-25 11:14:00 User 9854323

package snippet;

import java.util.Arrays;
import java.util.Comparator;

public class ComparatorUse {

    public static void main(String[] args) {
        Student[] persons = new Student[5];
        persons[0] = new Student("tom", 1, 88, 45);
        persons[1] = new Student("jack", 6, 80, 12);
        persons[2] = new Student("bill", 4, 68, 21);
        persons[3] = new Student("kandy", 2, 98, 34);
        persons[4] = new Student("lily", 5, 94, 20);
        System.out.println(" Data before sorting :");
        for (Student student : persons) {
            System.out.println(student);
        }
        // Here's the key 
        Arrays.sort(persons, new SortByNumber());
        System.out.println(" Sort by student number from low to high :");
        for (Student student : persons) {
            System.out.println(student);
        }
        // Here's the key 
        Arrays.sort(persons, new SortByScore());
        System.out.println(" According to the students' grades from high to low :");
        for (Student student : persons) {
            System.out.println(student);
        }
    }
    // Here's the key 
    static class SortByNumber implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.getNumber() - o2.getNumber();
        }
    }
    // Here's the key 
    static class SortByScore implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o2.getScore() - o1.getScore();
        }
    }
}

Output :

 Data before sorting :
Student[name:tom,age:45,number:1,score:88]
Student[name:jack,age:12,number:6,score:80]
Student[name:bill,age:21,number:4,score:68]
Student[name:kandy,age:34,number:2,score:98]
Student[name:lily,age:20,number:5,score:94]
 Sort by student number from low to high :
Student[name:tom,age:45,number:1,score:88]
Student[name:kandy,age:34,number:2,score:98]
Student[name:bill,age:21,number:4,score:68]
Student[name:lily,age:20,number:5,score:94]
Student[name:jack,age:12,number:6,score:80]
 According to the students' grades from high to low :
Student[name:kandy,age:34,number:2,score:98]
Student[name:lily,age:20,number:5,score:94]
Student[name:tom,age:45,number:1,score:88]
Student[name:jack,age:12,number:6,score:80]
Student[name:bill,age:21,number:4,score:68]

student class :

package snippet;


class Student {
   private String name;
   private int number;
   private int score;
   private int age;

   public Student(String name,int number,int score,int age){
       this.name = name;
       this.number = number;
       this.score = score;
       this.age = age;
   }
   @Override
   public String toString() {      
       return "Student[name:"+name+",age:"+age+",number:"+number+",score:"+score+"]";
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getNumber() {
       return number;
   }

   public void setNumber(int number) {
       this.number = number;
   }

   public int getScore() {
       return score;
   }

   public void setScore(int score) {
       this.score = score;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }
}
原网站

版权声明
本文为[User 9854323]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251056179788.html