Sunrise
Sunrise

Reputation: 23

Sorting a generic collection

I searched but couldn't find any answer on how to sort my own generic collection class.

I have these four classes:

2nd edit Simplified code, applied Evgeniy's suggestion but still doesn't sort, wired :\

A Track class:

public class Track {
    private Integer id;
    private String interpreter;
    private String title;
    Track(int id,String interpreter,String title) {
        this.id=id;
        this.interpreter=interpreter;
        this.title=title;
    }
    public String getInterpreter() {
        return this.interpreter;
    }
    public String getTitle() {
        return this.title;
    }
    public int getID() {
        return this.id;
    }
    public String getCompleteName() {
        return this.id+"\t"+this.interpreter.toString()+" - "+this.title.toString();
    }
}

A TrackContainer class:

import java.util.ArrayList;
import java.util.Iterator;

public class TrackContainer<T> extends ArrayList<T> {
    private static final long serialVersionUID = 1L;
    ArrayList<T> arraylist;

    TrackContainer() {
        arraylist = new ArrayList<T>();
    }

    public boolean insertTrack(T track) {
        if(arraylist.add(track)) 
            return true;
        return false;
    }
    public void listAllTracks(java.util.Iterator<T> iterat) {
        while(iterat.hasNext()) {
            System.out.println(iterat.next());
        }
    }

    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return arraylist.iterator();
    }
}

A TrackIDComparator class:

import java.util.Comparator;
public class TrackIDComparator implements Comparator<Track> {
    @Override
    public int compare(Track t1, Track t2) {
        // TODO Auto-generated method stub
        Comparable id1 = (Comparable)(t1.getID());
        Comparable id2 = (Comparable)(t2.getID());
        return id1.compareTo(id2);
    }
}

And finally a Main class:

import java.util.Collections;
import java.util.Iterator;
public class Main {
    public static void main(String[] args) {
        TrackContainer<Track> ltc = new TrackContainer<Track>();
        ltc.insertTrack(new Track(2,"trackname1","tracktitle1"));
        ltc.insertTrack(new Track(1,"trackname2","tracktitle2"));
        ltc.insertTrack(new Track(3,"trackname3","tracktitle3"));

        System.out.println("unsorted:");
        Iterator<Track> it = ltc.iterator();
        while(it.hasNext()) {
            System.out.println(it.next().getCompleteName());
        }

        System.out.println("sorted:");
        Collections.sort(ltc,new TrackIDComparator());
        Iterator<Track> it2 = ltc.iterator();
        while(it2.hasNext()) {
            System.out.println(it2.next().getCompleteName());
        }
    }
}

Output:

unsorted:
2   trackname1 - tracktitle1
1   trackname2 - tracktitle2
3   trackname3 - tracktitle3
sorted:
2   trackname1 - tracktitle1
1   trackname2 - tracktitle2
3   trackname3 - tracktitle3

Upvotes: 2

Views: 6384

Answers (4)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136142

Just add sort() method to TrackContainer

public void sort()  {
    Collections.sort(arraylist, new TrackIDComparator()); 
}

UPDATE

It's not clear why Track is generic and why T id; T interpreter; T title; should have the same type. I suggest to revise the design. Anyway,

class TrackIDComparator 

     public int compare(Track<T> o1, Track<T> o2) {
           if(o1.getID().hashCode() > o2.getID().hashCode())
               return 1;
...

is incorrect, change it as

public int compare(Track<T> o1, Track<T> o2) {
      Comparable id1 = (Comparable)(o1.getId());
      Comparable id2 = (Comparable)(o2.getId());
      return id1.compareTo(id2);
}

Upvotes: 3

RokL
RokL

Reputation: 2812

Easy way is to change TrackContainer

public class TrackContainer<T> extends ArrayList<Track<T>>

Remove the arraylist member variable and use this in any overriden methods.

Upvotes: 2

Buhake Sindi
Buhake Sindi

Reputation: 89209

TrackContainer isn't a List, so you cannot use Collections.sort(List<T>, Comparator<? super T> c) method.

Alternatively, you want to sort TrackContainer.arrayList field, so I would suggest to add a sort method that accepts a Comparator<Track<T>> to do the sorting internally.

Also, you would want to do new TrackIDComparator<String>() when binding Track to a type.

Upvotes: 0

amit
amit

Reputation: 178521

TrackContainer<T> is not a List - if you want to use Collections.sort(List,Comparator) on it, you should make it implements List<T> (and implement all interface methods, of course).

Upvotes: 1

Related Questions