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).
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);
}
}
And here is my simple Song
class;
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);
}
}
Can anyone help me figure out where I will call the reverseOrder()
method in the SongSort
class, as it won't compile?
Let's take a simple example we have a class Person with two fields name age and we want to sort an existing collection of persons based on their age so let's assume that we have a class Person with a constructor and add the persons into the list and then sort them unsing the method sort of collection :
and this this the impelemtation of Agecomparable :
the trick is to multiple the return method with -1 so the final result will be reversed: class AgeComparator implements Comparator{
so now we can get a reversed result :
One way to implement an reverse order comparator is to implement an Compartor-Delegate that invert the comparator result (by changing the order).
So the only thing you need to do is to use this delegate. For example:
If you need to use a Comparator which reverses the current order, just return a negative value in the
compare
method.Edit July 2015
As this answer still gets some attention, here a small update:
With Java SE 8 it's becoming easier to create a reversed comparator:
And you can, of course, also use the Streams framework: