我正在解决一个问题,我必须从输入文件中获取这些“歌曲艺术家对”并按字母顺序排序。排序准则如下:
我的问题是,当我对这些歌曲进行排序时,我能够正确地对这些歌手进行排序,但是在他们具有相同歌手的条件下,我无法对歌曲进行排序。
这是输入文件的样子:
Hello - Adele
Yesterday - The Beatles
Love Me Like You Do - Ellie Goulding
Hey Jude - The Beatles
Istanbul - They Might Be Giants
Run Code Online (Sandbox Code Playgroud)
我已经正确阅读了输入文件,但到目前为止,我的比较器仅按字母顺序对艺术家进行排序。这是我的比较器的外观:
public static class SongComparator implements Comparator<Song>{
public int compare(Song a, Song b){
return a.effectiveAuthor().compareTo(b.effectiveAuthor());
}
}
Run Code Online (Sandbox Code Playgroud)
(我创建了一个类来轻松跟踪歌曲及其艺术家。effectiveAuthor()方法返回作者的字符串,名称前没有“ The”)
当使用Song对象和比较器的数组调用Arrays.sort()时,这是我得到的输出:
Hello - Adele
Yesterday - The Beatles
Hey Jude - The Beatles
Love Me Like You Do - Ellie Goulding
Istanbul - They Might Be Giants
Run Code Online (Sandbox Code Playgroud)
这是经过适当排序的输出结果: …