3

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..

3
  • Although you may think that putting underscores in your identifiers makes them easier to read, to do so is strongly frowned upon in Java. They are really only used in identifiers in other languages such as C and SQL. Commented Nov 1, 2013 at 21:16
  • why not just use a comparator? Commented Nov 1, 2013 at 21:17
  • Hi Scott thanks for your reply. The solution then would be to edit the datamember prior to the sort() call? So I then guess that I would have to iterate through my MP3 catalogue and marking this member fo each track??? Commented Nov 1, 2013 at 21:22

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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) {
   ...
 }

Comments

-2

I suppose you could have different subclasses of MP3_Track for each kind of sorting, but THAT seems more cumbersome than having some data member to check to decide which sort to use, and complicates switching between sorting methods at run time.

I honestly don't see what the problem w/ using a data member to decide which kind of sort to do.

2 Comments

Hi many thanks for your reply. This was my first thought, but I can only see the following solution for this.
The problem with that is it violates the symmetry condition of compareTo. DO NOT DO THIS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.