Take the 2-minute tour ×

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) {
    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) {
            return true;
        return false;
    public void listAllTracks(java.util.Iterator<T> iterat) {
        while(iterat.hasNext()) {

    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> {
    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"));

        Iterator<Track> it = ltc.iterator();
        while(it.hasNext()) {

        Collections.sort(ltc,new TrackIDComparator());
        Iterator<Track> it2 = ltc.iterator();
        while(it2.hasNext()) {


2   trackname1 - tracktitle1
1   trackname2 - tracktitle2
3   trackname3 - tracktitle3
2   trackname1 - tracktitle1
1   trackname2 - tracktitle2
3   trackname3 - tracktitle3
share|improve this question
sort expects a List or a subclass of List as its first argument, which your custom list is not. –  assylias Dec 6 '12 at 12:18
Where's TrackContainer? You've only shown us ListTrackContainer. –  sshannin Dec 6 '12 at 12:19
Bascially it means you need TrackContainer to implement the List interface. –  amit Dec 6 '12 at 12:21
add comment

4 Answers

up vote 2 down vote accepted

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.

share|improve this answer
Couldn't have said it better myself, but you forgot mentioning how to actually sort the TrackContainer afterwards (by using Collections.sort method). –  Simon André Forsberg Dec 6 '12 at 12:44
Simon, I indeed have still problems applying U Mad's suggestion –  Sunrise Dec 6 '12 at 13:16
What is the problem? –  U Mad Dec 6 '12 at 13:30
How does Collections.sort(...) in Main class looks like? I also have no overridden methods in TrackContainer –  Sunrise Dec 6 '12 at 13:38
Collections.sort(ltc,new TrackIDComparator()); should work fine, provided the comparator is correctly written. You can use pastebin.com to show the code. –  U Mad Dec 6 '12 at 13:46
show 7 more comments

Just add sort() method to TrackContainer

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


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

      Comparable id1 = (Comparable)(o1.getId());
      Comparable id2 = (Comparable)(o2.getId());
      return id1.compareTo(id2);
share|improve this answer
Can you help me with U Mad's answer (it compiles now, but doesn't sort, wired) –  Sunrise Dec 6 '12 at 19:41
See my update, should help, but something is general design I guess –  Evgeniy Dorofeev Dec 6 '12 at 20:56
There's this error with your The method getId() is undefined for the type Comparable (changed to getID()) I can't solve it :\ –  Sunrise Dec 7 '12 at 10:03
rigth, my bad, will fix now, still the idea is the same –  Evgeniy Dorofeev Dec 7 '12 at 10:05
I simplified the classes and applied your suggestion but it still doesn't sort :\ –  Sunrise Dec 7 '12 at 12:16
add comment

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

share|improve this answer
I would suggest implementing a sort method inside the TrackContainer which uses Collections.sort on the internal list (if no need to support Collections.sort) –  Aviram Segal Dec 6 '12 at 12:25
amit, implementing all the List methods sounds like a lot of work. Aviram, Evgeniy's suggestion works. Still trying to use Collections.sort within the Main class. –  Sunrise Dec 6 '12 at 14:03
add comment

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.

share|improve this answer
add comment

Your Answer


