Solved

how to sort an arraylist by column?

Posted on 2004-08-25
20
252 Views
Last Modified: 2012-08-14
experts,

I need to sort an ArrayList of an OBject, which contains column A, B, C. I need to sort by column C of this object, how to do this? tried and no luck.

thanks.




0
Comment
Question by:panpanW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 5
  • 2
  • +1
20 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11894870
I am not sure I understand fully. What exactly do you mean "contains column A, B, C"? Can you elaborate?


Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11895097
You probably mean a List of ArrayList. You can use a custom Comparator:

import java.util.*;

public class ColumnSorter implements Comparator {
  int columnIndex;

  public ColumnSorter(int columnIndex) {
    this.columnIndex = columnIndex;
  }

  public int compare(Object o1, Object o2) {
    //return ((int[])o1)[columnIndex] - ((int[])o2)[columnIndex];
    java.util.List list1 = (java.util.List)o1;
    java.util.List list2 = (java.util.List)o2;
    return ((Comparable)list1.get(columnIndex)).compareTo((Comparable)list2.get(columnIndex));
  }

}



0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 60 total points
ID: 11895115
This assumes that the Lists contain elements that are Comparable (usually the case). You can use the class as follows:

Collections.sort(yourListOfLists, new ColumnSorter(2)); // sort by column 2
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:panpanW
ID: 11895186
thanks, what do you mean by Comparable?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11895287
Things like String, Integer, Date are Comparable
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11895292
What will your ArrayList contain?
0
 

Author Comment

by:panpanW
ID: 11895379
class test {

int a
string b
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 60 total points
ID: 11895446
OK. In that case, make it Comparable:


class Test implements Comparable {

      int a;
      String b;
      
      .....
      
      public int compareTo(Object other) {
            Test otherTest = (Test)other;
            return this.a - otherTest.a;
            // (Or however you want to sort them)
      }
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11895455
(That would sort on field 'a')
0
 
LVL 92

Expert Comment

by:objects
ID: 11898112
If the above comments have answered your question then please accept an answer, if not then post your further questions and we shall attempt to assist you further.
0
 

Author Comment

by:panpanW
ID: 11906156

thanks, but I am trying to put everything together and getting ClassCastException:

   
     public int compareTo(Object other) {
          Test otherTest = (Test)other;
          return this.a - otherTest.a;
          // (Or however you want to sort them)
     }

do you try to minus these two value? so, this will return a number? then, combined with above codes you wrote:

public class ColumnSorter implements Comparator {
  int columnIndex;

  public ColumnSorter(int columnIndex) {
    this.columnIndex = columnIndex;
  }

  public int compare(Object o1, Object o2) {
    //return ((int[])o1)[columnIndex] - ((int[])o2)[columnIndex];
    java.util.List list1 = (java.util.List)o1;
    java.util.List list2 = (java.util.List)o2;
    return ((Comparable)list1.get(columnIndex)).compareTo((Comparable)list2.get(columnIndex));
  }

}

Collections.sort(yourListOfLists, new ColumnSorter(2));
*********************************************
don't know why is getting ClassCastException??? thanks.l
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11906190
Have you made your Test class Comparable?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11906197
>>do you try to minus these two value? so, this will return a number?

Yes and yes
0
 

Author Comment

by:panpanW
ID: 11906241
i did. it looks like the exception comes from

public int compare(Object o1, Object o2) {
    //return ((int[])o1)[columnIndex] - ((int[])o2)[columnIndex];
    List list1 = (List)o1;
    List list2 = (List)o2;
    return ((Comparable)list1.get(columnIndex)).compareTo((Comparable)list2.get(columnIndex));
  }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11906294
OK. As the first two lines of that method, do

System.out.println(o1.getClass());
System.out.println(o2.getClass());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11906304
Oh and get rid of that commented-out line i stupidly left in ;-)
0
 

Author Comment

by:panpanW
ID: 11906385
it returns the "test" class, not the ArrayList of test....
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 60 total points
ID: 11906442
OK. You must have one dimension then. Just make this the method body:

    Comparable test1 = (Comparable)o1;
    Comparable test2 = (Comparable)o2;
    return test1.compareTo(test2);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11911014
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11918374
Thanks for closing the question :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 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