Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

how to sort an arraylist by column?

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
panpanW
Asked:
panpanW
  • 12
  • 5
  • 2
  • +1
3 Solutions
 
girionisCommented:
I am not sure I understand fully. What exactly do you mean "contains column A, B, C"? Can you elaborate?


Regards
0
 
CEHJCommented:
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
 
CEHJCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
panpanWAuthor Commented:
thanks, what do you mean by Comparable?

0
 
CEHJCommented:
Things like String, Integer, Date are Comparable
0
 
CEHJCommented:
What will your ArrayList contain?
0
 
panpanWAuthor Commented:
class test {

int a
string b
}
0
 
CEHJCommented:
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
 
CEHJCommented:
(That would sort on field 'a')
0
 
objectsCommented:
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
 
panpanWAuthor Commented:

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
 
CEHJCommented:
Have you made your Test class Comparable?
0
 
CEHJCommented:
>>do you try to minus these two value? so, this will return a number?

Yes and yes
0
 
panpanWAuthor Commented:
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
 
CEHJCommented:
OK. As the first two lines of that method, do

System.out.println(o1.getClass());
System.out.println(o2.getClass());
0
 
CEHJCommented:
Oh and get rid of that commented-out line i stupidly left in ;-)
0
 
panpanWAuthor Commented:
it returns the "test" class, not the ArrayList of test....
0
 
CEHJCommented:
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
 
CEHJCommented:
:-)
0
 
objectsCommented:
Thanks for closing the question :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 12
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now