我正在创建一个简单的程序来了解 Java Comparator 类。我已经按Arraylist
顺序对列表进行了排序,但是现在我想按降序对列表进行排序,但是.reverseOrder()
由于我使用了一个实现的内部类Comparator<Song>
(song 是一个包含 getter 和 setter 方法的歌曲类),所以在调用该方法的位置有问题。
这是我的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
,因为它不会编译?