Sorting a List of objects without implements Comparable

Posted on 2007-10-12
Last Modified: 2012-05-05
I'm tying to figure out how to sort a list of objects ex List<Test> testList. Usually I'd do "public class Test implements Comparable<Test>" and use the CompareTo method as Colllections.sort(testList).

But for other reasons my object can not impement Comparable. What is the easiest way to sort a list then?

I need the objects in my testList sorted on the variable sortNum.

test class looks like this:
public class Test() {
  private String Name;
  private int sortNum;
Question by:kristian_gr
    LVL 8

    Expert Comment

    you can use Comparator instead

    Collections.sort(yourList, new Comparator<Test>() {

                      public int compare(Test t1, Test t2) {
                                    // check whether these two Test are equal (0), greater than (1)or less than(-1)
                            return 0;

    LVL 8

    Accepted Solution

    Collections.sort(yourList, new Comparator<Test>() {

                      public int compare(Test t1, Test t2) {
                                    // check whether these two Test are equal (0), greater than (1)or less than(-1)
                            return 0;


    sorry I forget a ) here

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Suggested Solutions

    Title # Comments Views Activity
    seriesUp challenge 7 79
    scoresClump  challenge 31 85
    endX challenge 2 30
    countAbc challenge 9 35
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now