Java Comparator 使用 .reverseOrder() 但带有一个内部类

2022-09-01 13:36:51

我正在创建一个简单的程序来学习Java Comparator类。我已经按顺序排序了一个,但现在我想按降序对列表进行排序,但是在调用方法的位置遇到了问题,因为我使用了一个实现的内部类(song是一个包含getter和setter方法的歌曲类)。Arraylist.reverseOrder()Comparator<Song>

这是我的课程,其中包含排序过程等;SongSort

import java.util.*;
import java.io.*;

public class SongSort
{
    ArrayList<Song> songList = new ArrayList<Song>();

    public void main(String[] args)
    {
        new SongSort().go();
    }

    class ArtistCompare implements Comparator<Song>
    {
        public int compare(Song one, Song two)
        {
            return one.getRating().compareTo(two.getRating());
        }
    }


    public void go()
    {

        getSongs();
        System.out.println(songList);
        //Collections.sort(songList); 
        System.out.println(songList);

        ArtistCompare artistCompare = new ArtistCompare();
        Collections.sort(songList, artistCompare);
        System.out.println(songList);
    }



    public void getSongs()
    {
        try{
            File file = new File("SongListMore.txt");
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;

            while((line = reader.readLine()) != null)
               {
                   addSong(line);
               }
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }

        public void addSong(String lineToParse)
        {
            String [] tokens = lineToParse.split("/");
            Song nextSong = new Song(tokens[0],  tokens[1], tokens[2], tokens[3]);
            songList.add(nextSong);

    }

}

这是我的简单课程;Song

public class Song //implements Comparable<Song>
{
    private String title;
    private String artist;
    private String rating;
    private String bpm;

    public Song(String t, String a, String r, String b)
    {
        title = t;
        artist = a;
        rating = r;
        bpm = b;
    }

    public String getTitle()
    {
        return title;
    }

    public String getArtist()
    {
        return artist;
    }
    public String getRating()
    {
        return rating;
    }
    public String getBpm()
    {
        return bpm;
    }

    public String toString()
    {
       return ("Title : " + title + "," +  " Artist : " + artist +  " Rating : " + rating);
    }
}

任何人都可以帮我弄清楚我将在类中调用该方法的位置,因为它不会编译?reverseOrder()SongSort


答案 1
ArtistCompare artistCompare = new ArtistCompare();
Collections.sort(songList, Collections.reverseOrder(artistCompare));

编辑2015年7月

由于这个答案仍然受到一些关注,这里有一个小的更新:

使用 Java SE 8,创建反向比较器变得越来越容易:

Comparator<Song> songRatingComparator = Comparator.comparing(Song::getRating);
Collections.sort(songList, songRatingComparator.reversed());

当然,您也可以使用Streams框架:

List<Song> sortedSongList = songList.stream()
.sorted(Comparator.comparing(Song::getRating).reversed())
.collect(Collectors.toList());

答案 2

实现反向顺序比较器的一种方法是实现比较器-委托,该比较器委托可反转比较器结果(通过更改顺序)。

public class ReverseOrder<T> implements Comparator<T> {
  private Comparator<T> delegate;
  public ReverseOrder(Comparator<T> delegate){
    this.delegate = delegate;
  }

  public int compare(T a, T b) {
    //reverse order of a and b!!!
    return this.delegate.compare(b,a);
  }
}

因此,您唯一需要做的就是使用此委托。例如:

  Comparator myComparator = new myComparator();
  List list = ...;
  List reverse = new ArrayList(list);

  //acceding
  Collections.sort(list, myComparator);

  //descending
  Collections.sort(list, new ReverseOrder(myComparator));

推荐