在数组中按比例对对象进行排序 导入java.io.File; 导入java.util.Scanner; 公共类主类{ 公共静态Winner[]listOfWinners; 公共静态void loadFromFile() { 试一试{ //创建扫描仪实例并提供指向txt文件的文件实例 扫描仪输入=新扫描仪(新文件(“WorldSeriesWinners.txt”); //获取团队的数量 int years=input.nextInt(); input.nextLine();//移到下一行 //创建数组 listOfWinners=新的获胜者[年]; //对于文本文件中的每年 对于(int index=0;index

在数组中按比例对对象进行排序 导入java.io.File; 导入java.util.Scanner; 公共类主类{ 公共静态Winner[]listOfWinners; 公共静态void loadFromFile() { 试一试{ //创建扫描仪实例并提供指向txt文件的文件实例 扫描仪输入=新扫描仪(新文件(“WorldSeriesWinners.txt”); //获取团队的数量 int years=input.nextInt(); input.nextLine();//移到下一行 //创建数组 listOfWinners=新的获胜者[年]; //对于文本文件中的每年 对于(int index=0;index,java,arrays,sorting,Java,Arrays,Sorting,,您可以利用 Arrays.sort(listOfWinners,新的比较器(){ @凌驾 公共整数比较(赢家o1,赢家o2){ 返回o1.team.compareTo(o2.team); } }); 将其放入列表中,只需调用Collections.sort()?或只需执行Arrays.sort()即可我不能使用Array.sort,因为数组同时包含数字和字母。您可以使用不同的数据结构,在插入项目时对项目进行排序。是的,您可以使用该方法。您可以给它一个比较器,如下面的答案所示。 import j

,您可以利用

Arrays.sort(listOfWinners,新的比较器(){
@凌驾
公共整数比较(赢家o1,赢家o2){
返回o1.team.compareTo(o2.team);
}
});

将其放入列表中,只需调用
Collections.sort()
?或只需执行
Arrays.sort()即可
我不能使用Array.sort,因为数组同时包含数字和字母。您可以使用不同的数据结构,在插入项目时对项目进行排序。是的,您可以使用该方法。您可以给它一个比较器,如下面的答案所示。
import java.io.File;
import java.util.Scanner;

public class MainClass {

    public static Winner [] listOfWinners;

    public static void loadFromFile()
    {
        try{
            //Create instance of Scanner and provide instance of File pointing to the txt file
            Scanner input = new Scanner(new File("WorldSeriesWinners.txt"));

            //Get the number of teams
            int years = input.nextInt();
            input.nextLine();//move to the next line

            //Create the array
            listOfWinners = new Winner[years];

            //for every year in the text file
            for(int index = 0; index<years; index++)
            {
                //Get the year
                int year = input.nextInt();
                input.skip("    ");
                //Get the team
                String team = input.nextLine();

                //Create an instance of Winner and add it to the next spot in the array
                listOfWinners[index] = new Winner(team,year);
            }
        }catch(Exception e)
        {
            System.out.println("Something went wrong when loading the file!");
            System.out.println(e.toString());
            System.exit(0);
        }
    }

    public static void sortByTeamName()
    {

    }
Arrays.sort(listOfWinners, new Comparator<Winner>() {

            @Override
            public int compare(Winner o1, Winner o2) {

                return o1.team.compareTo(o2.team);
            }
        });