Java Multi-Level Comparator

后端 未结 2 1214
别那么骄傲
别那么骄傲 2021-01-24 07:31

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:

  • Sh
2条回答
  •  [愿得一人]
    2021-01-24 07:55

    In your compare method, compare the song titles if the artists names are identical. Like this:

    public static class SongComparator implements Comparator{
        public int compare(Song a, Song b){
            int rslt a.effectiveAuthor().compareTo(b.effectiveAuthor());
            if (rslt ==0)
            {
                // compare song names
                rslt = a.getSongName().compareTo(b.getSongName());
            }
            return rslt;
        }
    }
    

提交回复
热议问题