Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple sort criteria on ArrayList

Tags:

java

sorting

I've been given a task of creating a MP3 catalogue with all the usual functions. I have a MP3_Track, MP3_UserInterface and Main classes.

public class MP3_Track implements Comparable<MP3_Track>{

private int trackNo;
private String artistName;
private String albumName;
private String trackLength;

MP3_Track(int no, String artist, String album, String length){
    this.trackNo = no;
    this.artistName = artist;
    this.albumName = album;
    this.trackLength = length;

// setters & getters

@Override
public String toString(){
        return (" Track Number : " + trackNo + "\n Artist Name  : " + artistName + "\n Album Name   : " + albumName +"\n Track Length : " + trackLength);
}

@Override
public int compareTo(MP3_Track aTrack){
    int result;
    try{
        result = artistName.compareTo(aTrack.artistName);
    }
    catch(UnsupportedOperationException e){
        throw new UnsupportedOperationException("Not supported here.");
    }
    return result;
  }
}

My main class contains:

static void deleteTrack()
static MP3_Track addTrack()
static void moveTrack()
static void exploreTracks()...etc

Main calls on the relevant methods depending what the user enters at the console.

Such as: Collections.sort(myTracks); myTracks is a collection of MP3_Tracks and at the moment mt MP3_Track overrides the compareTo() method but as you can see it only sorts it by artistName!!! I want the user to have the choice of how the tracks are sorted etc. I thought that maybe I could have a data mamber in MP3_Track which could flag what type of sort to implement then some if's in compareTo method to return the required result. This solution seems somewhat cumbersome, another possibility I thought of was maybe implementing a custom interface!!!

Any suggestions as to a slick workaround are much appreciated???

Many thanks i advance, great site BTW..

like image 418
TripVoltage Avatar asked May 05 '26 19:05

TripVoltage


2 Answers

You need to look at the Collections.sort() method. You can pass in an implementation of the Comparator interface and it will use that to sort.

For an example see http://www.vogella.com/articles/JavaCollections/article.html#collectionssort

  • Collections docs http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

  • Comparator docs http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

like image 195
pjp Avatar answered May 08 '26 09:05

pjp


You can pass a custom comparator to Collections.sort based on user input

For example --

if ( "artistName".equals(sortType) ) {
    Collections.sort(myList, new Comparator<MP3_Track>() {
        @Override
        public int compare(MP3_Track track1, MP3_Track track2) {
          // use artist name to compare
        }
    });
 }
 else if ("albumName".equals(sortType) {
   ...
 }
like image 20
Kal Avatar answered May 08 '26 07:05

Kal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!