Java多级比较器 [英] Java Multi-Level Comparator

查看:220
本文介绍了Java多级比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个问题,我必须从输入文件中获取这些歌曲艺术家对",并按字母顺序进行排序.排序准则如下:

I am working on a problem where I must take these "Song-artist pairs" from an input file and sort alphabetically. The guidelines to the sorting goes like so:

  • 应首先按作者姓名对歌曲艺术家对进行排序.
  • 按艺术家排序后,如果同一位艺术家有多首歌曲,则也应按字母顺序对其进行排序.
  • 如果艺术家的名字以"The"开头,则出于排序目的将其忽略.

我的问题是,当我对这些歌曲进行排序时,我能够对它们进行正确排序,但是在他们具有相同艺术家的条件下,我无法对歌曲进行排序.

My problem is that when I am sorting these, I am able to get the artists sorted properly, but then I cannot sort the songs under the conditions that they have the same artist.

这是输入文件的外观:

    Hello - Adele
    Yesterday - The Beatles
    Love Me Like You Do - Ellie Goulding
    Hey Jude - The Beatles
    Istanbul - They Might Be Giants

我已经正确阅读了输入文件,但是到目前为止,我的比较器仅按字母顺序对艺术家进行排序.这是我的比较器的外观:

I have properly read through the input file, but so far my comparator only sorts the artists alphabetically. This is what my comparator looks like:

    public static class SongComparator implements Comparator<Song>{
        public int compare(Song a, Song b){
            return a.effectiveAuthor().compareTo(b.effectiveAuthor());
        }
    }

(我创建了一个类来轻松跟踪歌曲及其艺术家.有效的Author()方法返回作者的字符串,名称前没有"The")

(I have created a class to easily keep track of the songs and their artists. The effectiveAuthor() method returns the string of the author without the "The " in front of the name)

当使用Song对象和比较器的数组调用Arrays.sort()时,这是我得到的输出:

When calling Arrays.sort() with the array of Song objects and comparator, this is the output I get:

    Hello - Adele
    Yesterday - The Beatles
    Hey Jude - The Beatles
    Love Me Like You Do - Ellie Goulding
    Istanbul - They Might Be Giants

这是经过正确排序的输出的样子:

This is what the output with the proper sorting would look like:

    Hello - Adele
    Hey Jude - The Beatles
    Yesterday - The Beatles
    Love Me Like You Do - Ellie Goulding
    Istanbul - They Might Be Giants

我最初的想法是遍历整个数组,找到同一位艺术家的歌曲,然后找到一种对它们进行排序并将其重新插入该数组的方法,这有点复杂.有人告诉我,可以通过使用更全面的比较器来对艺术家和歌曲名称进行排序,并且只需要对所有Song对象调用一次Arrays.sort.

My original idea was to loop through the array and find the songs with the same artist, and find a way to sort them and re-insert those back into that array, which is a bit complex. I was told that I could use a more comprehensive comparator by having them sort both the artist and song names, and that I would only need to call Arrays.sort once for all of the Song objects.

有人可以告诉我如何制作一个适用于这种情况的更全面的比较器吗?我目前仅知道我可以使用比较器的两种方式,即比较数值(即,如果a> b返回-1,如果a == b,则返回0,如果a< b,则返回1),以及字符串值(又名a.compareTo(b)),但我不知道如何制作更精细的比较器,以帮助我先按歌手然后按歌曲名称排序.

Can somebody show me how to make a more comprehensive comparator that would pertain to this situation? I currently only know two ways that I can use a comparator, which is to compare numerical values(aka if a > b return -1, if a == b, return 0, and if a < b, return 1), and String values(aka a.compareTo(b)), but I do not know how I would be able to make a more elaborate comparator to help me out with being able to sort by artist first and then the song name.

谢谢

PS:是我提到的Java程序的pastebin,如果您想进一步了解我正在尝试解决的问题. 是我正在解析的文本文件的样子,其中第一行是测试编号案例,然后是带有歌曲艺术家对数量的数字.

PS: This is a pastebin to the java program I mentioned, if you would like more insight on the problem I am trying to solve. This is what the text file would look like that I am parsing through, where the first line is the number of test cases, followed by a number with the amount of song-artist pairs.

推荐答案

比方说,您说成功创建的类称为SongArtistPair,它具有一个名为effectiveAuthor()的方法,该方法返回作者的姓名而没有,以及返回歌曲名称的方法getSongName().您可以使用Java 8 Comparator API提供的这种模式.

Let's say the class you said you managed to create is called SongArtistPair and it has a method called effectiveAuthor()which returns the author's name without the The, and a method getSongName() which returns the name of the song. You can use this pattern offered by the Java 8 Comparator API.

Comparator<SongArtistPair> comp = Comparator.comparing(SongArtistPair::effectiveAuthor).thenComparing(SongArtistPair::getSongName);

之后,只需正常使用comp

检查Comparator API文档是否有更酷的内容此处

Check Comparator API docs for more cool stuff HERE

这篇关于Java多级比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆