Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:55:30+00:00 2026-06-15T13:55:30+00:00

I searched but couldn’t find any answer on how to sort my own generic

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:55:31+00:00Added an answer on June 15, 2026 at 1:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched for an answer but couldn't find it anywhere. My question is
I searched StackOverflow but couldn't find the answer to this question. Suppose I have
I have searched everywhere but I couldn't find my answer, is there a way
I have searched the web but couldn't find any detailed idea about Native OS
I've searched but couldn't find any results (my terminology may be off) so forgive
I searched before but couldn't find any answers. I am somewhat new to c++,
I searched around but couldn't find exactly what I'm looking for. I have a
What is stack unwinding? Searched through but couldn't find enlightening answer!
I've searched within the site, but couldn't find my answer.. I'd like to start
Hy there I searched a lot on google, but couldn't find an answer. Maybe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.