查找和排序

题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
都按先录入排列在前的规则处理。

示例:
jack      70
peter     96
Tom       70
smith     67

从高到低  成绩
peter     96
jack      70
Tom       70
smith     67

从低到高

smith     67

jack      70
Tom      70
peter     96

import java.util.Scanner;
 
public class Main{
    private static String[] names;
    private static int[] grades;
    private static void swap(int i, int j) {
        String tempS = names[j];
        int tempG = grades[j];
        names[j] = names[i];
        grades[j] = grades[i];
        names[i] = tempS;
        grades[i] = tempG;
    }
    public static void sort(int flag) {
        if(flag==0){
        for (int i = 1; i < names.length; i++)
            for (int j = i; j > 0 ; j--) {
                if(grades[j]>grades[j-1])
                swap(j, j - 1);
            }
    }else if(flag==1){
           for (int i = 1; i < names.length; i++)
            for (int j = i; j > 0 ; j--) {
                if(grades[j]<grades[j-1])
                swap(j, j - 1);
            }
        }
    }
    public static void main(String[] args) {        // TODO Auto-generated method stub       
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int n = input.nextInt();
            int flag = input.nextInt();
            names = new String[n];
            grades = new int[n];
            for (int i = 0; i < n; i++) {
                names[i] = input.next();
                grades[i] = input.nextInt();
            }
            sort(flag);
            for (int i = 0; i < n; i++) {
                System.out.println(names[i] + " " + grades[i]);
            }
        }
    }
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {             
             //输入人数       
            int numPeople = scanner.nextInt();            
             //排序策略           
            int option = scanner.nextInt();                        
             //输入人员的姓名和成绩             
            List<Student> stuList = new ArrayList<>();
            for (int i = 0; i < numPeople; i++) {
                stuList.add(new Student(scanner.next(), scanner.nextInt()));
            }                          //降序         
            if (option == 0) {
                stuList.sort((o1, o2) -> o2.score - o1.score);
            } else if (option == 1)//升序             
            {
                stuList.sort((o1, o2) -> o1.score - o2.score);
            }
            for (Student student : stuList) {
                System.out.println(student.name + " " + student.score);
            }
        }
    }
}

class Student {
    public String name;
    public int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }
}