简体   繁体   English

按字母顺序对字符串数组进行排序。 如何按每行中间的数字对同一数组进行排序

[英]Sorted an array of strings alphabetically. How do I sort the same array by a number in the middle of each line

This is my list of Players and their Scores that I need to input from a text file, sort alphabetically, and then output to a new file. 这是我需要从文本文件中输入,按字母顺序排序然后输出到新文件的播放器及其分数的列表。 I then have to take the same list and sort it based on the average score at the end of each line and then output that sorted list to a file where the players are sorted by their scores. 然后,我必须采用相同的列表,并根据每行末尾的平均得分对其进行排序,然后将排序后的列表输出到一个文件中,在此文件中,玩家将按其得分进行排序。 This is the list of players: 这是球员名单:

Bruce 127 布鲁斯127

Elayna 144 伊莱娜144

Lisa 153 丽莎153

Marcus 188 马库斯188

Amber 133 琥珀色133

Ryan 149 瑞安149

Dorian 099 多利安099

Joel 113 乔尔113

Kelly 097 凯利097

This is my code so far. 到目前为止,这是我的代码。 There are some methods and lines that are totally pointless that I haven't cleared out yet because I'm not sure if I need them or not. 有些方法和行完全没有意义,我尚未清除,因为我不确定是否需要它们。

public class MembersAndScores 
{

    //trying to create a method to read the input file, examine the first character in each line, and sort accordingly to get an alphabetically ordered list

    public static void insert(int[] x, int i)
    {
        int temp = x[i];
        int j = i - 1;
        while (j >= 0 && temp <x[j])
        {
            x[j+1] = x[j];
            j--;
        }
        x[j+1] = temp;
    }

    //Uses the insert method above by calling it to sort the array of strings below that hold the names and scores of the bowlers.
    public static void insertionsort(int[] x)
    {
        for (int i = 1; i < 9; i++)
            insert(x, i);
    }


    public static void main(String[] args) throws IOException
    {
        File inputFile = new File("players_scores.txt");
        if (!inputFile.exists())
        {
            System.out.println("File players_scores.txt was not found.");
            System.exit(0);
        }
        //setting up Scanner to read the input File, players_scores.txt. Created a String array to store the lines of text in.
        Scanner input = new Scanner(inputFile);
        String line;
        String[] array1 = new String[9];

        for (int i = 0; i <9; i++)
        {
            line = input.nextLine();
            array1[i]= line;
        }
        //calling my sorting method to sort the lines stored inside array1[0] through array1[8].
        Arrays.sort(array1);

        //begins the process of creating the file where data is sorted by name. program checks for txt file and creates it if missing.  
        File sortName = new File("sortedbyname.txt");
        PrintWriter output = new PrintWriter(sortName);

        //checks to see if the file exists and creates it if it doesn't
        if(!sortName.exists())
        {
            sortName.createNewFile();
        }

        for (int i = 0; i < 9; i++)
        {
            output.println(array1[i]);
        }
        output.close(); 
        //close output so it finishes up and prints the stored lines to the document.


        //begins the process of creating the file where data is sorted by score
        File sortScore = new File("sortedbyscore.txt"); 
        PrintWriter output2 = new PrintWriter(sortScore);
        String line2;


        Arrays.sort(array1);

        //computer checks to see if it doesn't exist and creates it if it doesn't
        if(!sortScore.exists())
        {
            sortScore.createNewFile();
        }

        for (int i = 0; i < 9; i++)
        {
            output2.println(array1[i]);
        }

        output2.close();
        input.close();
    }

}

I see that you can sort the array1 using array1.sort() this will only sort the array alphabetically. 我看到您可以使用array1.sort()对array1进行排序,这只会按字母顺序对数组进行排序。 If you want to sort by the average test score I would do something like 如果您想按平均考试成绩排序,我会做类似的事情

    int avgScore[] = new int[9]; //make the nine whatever value you need.
    for(int i=0; i<avgScore.length;i++){
         avgScore[i] = Integer.parseInt(array1[i].substring(array1[i].length-                       3,array1[i].length));
    }
    avgScore.sort();

Substring is a very useful method in java and could be used to solve at least on of your issues. 子字符串是Java中非常有用的方法,至少可用于解决您的问题。 You can read about it here: http://www.tutorialspoint.com/java/java_string_substring.htm 您可以在这里阅读有关内容: http : //www.tutorialspoint.com/java/java_string_substring.htm

You can create Comparator: 您可以创建比较器:

static class ComparatorByScore implements Comparator<String> {

    public int compare(String o1, String o2) {
        Integer first = Integer.parseInt(o1.split(" ")[1]);
        Integer second = Integer.parseInt(o1.split(" ")[1]);
        return first.compareTo(second);
    }
}

and then use it in your code: 然后在您的代码中使用它:

Arrays.sort(array1, new ComparatorByScore());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java中按字母顺序对字符串数组进行排序? - How do I sort an array of strings alphabetically in java? 我试图按字母顺序对数组排序。 当我运行程序时,它从不交换名称。 我需要做什么来修复程序? - Im trying to sort an Array Alphabetically. When i run the program it never swaps the names. What do i need to do to fix my program? 如何在 Java 中按字母顺序对数组中的自定义对象进行排序? - How do I sort custom objects alphabetically in an array in Java? 如何在Java中按第二个单词按字母顺序对字符串数组进行排序 - How to sort an array of strings alphabetically by second word in Java 如何根据每个字符串的不同部分对字符串数组进行排序? - How do I sort an array of strings based on different parts of each string? 按字母顺序对字符串数组进行排序 - Sorting an array of strings alphabetically 如何在冒泡排序中执行 output 遍数直到数组排序? - How to output number of passes performed in bubble sort till the array is sorted? 按字母顺序对数组排序 - Sort an array alphabetically 给定数字N和N个字符串数组,请基于每个字符串中的元音数量以降序对字符串进行排序 - Given a number N and an array of N strings, sort the strings based on number of vowels in each of the strings in the descending order 如何按频率而不是按字母顺序排列字符串数组 - How to order an array of Strings by frequency, but not alphabetically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM