问题描述
我正在创建一个简单的程序来了解Java Comparator类.我已经按顺序对Arraylist
进行了排序,但是现在我想按降序对列表进行排序,但是由于使用了实现Comparator<Song>
的内部类(歌曲是一首歌,所以在调用.reverseOrder()
方法时遇到了问题包含getter和setter方法的类.
I am creating a simple program to learn about the Java Comparator class. I have sorted an Arraylist
into order but now I want to sort the list in descending order but am having problems in where to call the .reverseOrder()
method as I have used an inner class that implements Comparator<Song>
(song being a song class which houses getters and setter methods).
这是我的SongSort
类,其中包含排序过程等;
Here is my SongSort
class which houses the sorting process etc.;
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);
}
}
有人可以帮我弄清楚我将在SongSort
类中调用reverseOrder()
方法的地方,因为它无法编译吗?
Can anyone help me figure out where I will call the reverseOrder()
method in the SongSort
class, as it won't compile?
推荐答案
ArtistCompare artistCompare = new ArtistCompare();
Collections.sort(songList, Collections.reverseOrder(artistCompare));
编辑2015年7月
由于此答案仍然受到关注,因此这里进行了一个小更新:
As this answer still gets some attention, here a small update:
使用Java SE 8,创建反向比较器变得更加容易:
With Java SE 8 it's becoming easier to create a reversed comparator:
Comparator<Song> songRatingComparator = Comparator.comparing(Song::getRating);
Collections.sort(songList, songRatingComparator.reversed());
您当然也可以使用Streams框架:
And you can, of course, also use the Streams framework:
List<Song> sortedSongList = songList.stream()
.sorted(Comparator.comparing(Song::getRating).reversed())
.collect(Collectors.toList());
这篇关于Java比较器使用.reverseOrder()但具有内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!