Arrays.sort函数的重写

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class ex3 {
     
    //7.17
    public static void main(String[] args) {
     
        Scanner in = new Scanner(System.in);
    int amount=in.nextInt();
    in.nextLine();
    stu[] student = new stu[amount];
    int i;
    String m;
    int n;

    for (i = 0;i<amount;i++){
     
        m=in.next();
        n=in.nextInt();

        student[i]=new stu(m,n);

    }

    Arrays.sort(student);

    for (i=0;i<amount;i++){
     
        System.out.println(student[i].getName()+student[i].getAchievement());
    }

    }
}
class stu implements Comparable<stu> {
      //实现capable中的这个接口 进行Arrays.sort函数的重写 
    String name;
    int achievement;
    public stu(String name, int achievement) {
     
        this.name = name;
        this.achievement = achievement;
    }

    public String getName() {
     
        return name;
    }

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

    public int getAchievement() {
     
        return achievement;
    }

    public void setAchievement(int achievement) {
     
        this.achievement = achievement;
    }




    @Override
    public int compareTo(stu o){
     
        return o.achievement-achievement;//1与-1 决定 正序还是倒序
    }

}


你可能感兴趣的:(记录)