panpanW
asked on
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.
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.
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(col umnIndex)) .compareTo ((Comparab le)list2.g et(columnI ndex));
}
}
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(col
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, what do you mean by Comparable?
Things like String, Integer, Date are Comparable
What will your ArrayList contain?
ASKER
class test {
int a
string b
}
int a
string b
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(That would sort on field 'a')
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.
ASKER
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(col
}
}
Collections.sort(yourListO
**************************
don't know why is getting ClassCastException??? thanks.l
Have you made your Test class Comparable?
>>do you try to minus these two value? so, this will return a number?
Yes and yes
Yes and yes
ASKER
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(col umnIndex)) .compareTo ((Comparab le)list2.g et(columnI ndex));
}
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(col
}
OK. As the first two lines of that method, do
System.out.println(o1.getC lass());
System.out.println(o2.getC lass());
System.out.println(o1.getC
System.out.println(o2.getC
Oh and get rid of that commented-out line i stupidly left in ;-)
ASKER
it returns the "test" class, not the ArrayList of test....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
Thanks for closing the question :)
Regards