简体   繁体   English

如何在Java中按第二个单词按字母顺序对字符串数组进行排序

[英]How to sort an array of strings alphabetically by second word in Java

I have a little problem with an assignment I'm working on. 我正在处理的作业有一个小问题。 Basically, I have a file, with Student ID's and their first name in the following format: 基本上,我有一个文件,带有以下格式的学生证和名字:

17987 Beth 17987贝丝

17950 Clark 17950克拉克

17936 Aaron 17936亚伦

I put the contents of the file in an array, and I need it sorted by the name, not the ID. 我将文件的内容放在一个数组中,我需要按名称而不是ID对其进行排序。 If I use Arrays.sort(myArray) it will sort it automatically by ID. 如果我使用Arrays.sort(myArray) ,它将按ID自动排序。 I'm having a hard time understanding the Comparator, so if you could explain it step by step, it would be easier for me. 我很难理解比较器,因此,如果您可以逐步解释它,对我来说会更容易。 Thank you! 谢谢!

You need to provide a Comparator that will look at the String s passed to it, and they must have an understanding of what they are looking for. 您需要提供一个Comparator ,它可以查看传递给它的String ,并且他们必须了解所要查找的内容。 You can do this in a few different ways, but, if you know for certain about the content of the strings, then you can split the String s based on their space and compare the second value. 您可以通过几种不同的方式来执行此操作,但是,如果您对字符串的内容有所了解,则可以根据String的空间split String并比较第二个值。 Alternatively, you could use a regular expression to extract those details. 另外,您可以使用正则表达式提取这些详细信息。

class SecondWordComparator implements Comparator<String>
{
    @Override
    public int compare(String s1, String s2)
    {
        String[] a1 = s1.split(" ");
        String[] a2 = s2.split(" ");

        // you should ensure that there are actually two elements here
        return a1[1].compareTo(a2[1]);
    }
}

Use TreeMap , the entries will be sorted by key. 使用TreeMap ,条目将按键排序。

Eg: 例如:

SortedMap<String, Integer> srtdMap  = new TreeMap<String, Integer>();

srtdMap.put("Beth", 17987);
srtdMap.put("Aaron", 17936 );
srtdMap.put("Clark", 17950);

//key set always returns the same order by name
for(String name : srtdMap.keySet())
{
    int id = srtdMap.get(name);
    System.out.println("name = " + name + ", ID is " + id);
}

What you can do is run a for loop that would rearrange all the contents of the Array into a final array. 您可以做的是运行一个for循环,该循环会将Array的所有内容重新排列为最终的数组。 Two for loops would be required. 将需要两个for循环。 The first for loop will go through the Array and the for loop inside the first will look for the first letter and add it to the final Array/Arraylist . 第一个for循环将遍历Array,第一个内部的for循环将查找第一个字母并将其添加到最终的Array / Arraylist中。

For a object-oriented approach I would parse the file into a new class, Student , and save those in an Array(List). 对于面向对象的方法,我将文件解析为一个新类Student ,并将其保存在Array(List)中。 The class could extend Comparable<Student> There, you can separate the int ID and the String name and have the compareTo(Student student) method return name.compareTo(otherStudent.name) . 该类可以扩展Comparable<Student>在那里,您可以将int IDString name分开,并具有compareTo(Student student)方法的返回name.compareTo(otherStudent.name) Then you can call the sort method and it will sort it as wanted. 然后,您可以调用sort方法,它将根据需要对其进行排序。

As such: 因此:

public class Student implements Comparable<Student> {
    private int id;
    private String name;
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public int compareTo(Student student) {
        return name.compareTo(student.name);
    }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM