This week's book giveaway is in the Security forum.We're giving away four copies of Penetration Testing Basics and have Ric Messier on-line!See this thread for details.
Win a copy of Penetration Testing Basics this week in the Security forum!

# sorting a 2d arraylist

Andrew Goh
Greenhorn
Posts: 4
is there anyway to sort the whole 2d arraylist according to one column?
for example .. second column contains a String date...the third contains a String name... i want to sort the arraylist according to the date

Michael Morris
Ranch Hand
Posts: 3451
Hi Andrew,
When you say 2d arraylist, what exactly do you mean? The collection ArrayList is a sequential list of single objects backed by an array. Give us a little more info on the type of data structure you are using.
Michael Morris

Jeya Balaji
Ranch Hand
Posts: 40
I would have a seperate one dim integer array. The size of this array is equal to the number of rows (say n) of the 2D.
This int array holds the index of the 2D rows. So, filled with 1 to n.
By performing usual sorting method and changing this index array should do the trick.
After sorting, the 2D array will be intact - none of the rows or data will be exchanged.
Only the index array will go thru change. The index array might be like
5, 4,7,2,8,3 ...
which indicates the sorted position of the 2D array ...

Someone pls comment this method or suggest better one ...

Andrew Goh
Greenhorn
Posts: 4
Michael Morris ,
my 2d arraylist means that i have a array list with x number of rows and 2 columns, one for date and one for name. I wish to sort the whole arraylist according to the date found in column 1.
i create my arraylist in the following way.
ArrayList t = new ArrayList();
to add data... i use the following method (only an example)
ArrayList t1 = (ArrayList) t.get(0);
t1.add(a); // a represents the date
I have a problem sorting this arraylist.

Michael Morris
Ranch Hand
Posts: 3451
Hi Andrew,
By the way, welcome to JavaRanch. Umm, this is an intereresting problem. Not that there isn't a way to grind out a sort on your data structure, but it begs the question why should the elements of the primary ArrayList be themselves ArrayLists if each of those lists only contains a Date and Name? If that is the case then it would seem to make more since to create a class that encapsulated those two strings and implemented the Comparable interface. Here's what I mean:

And here's the output:

Now that is somewhat contrived in that it is a pure ASCII sort so 03-29-2003 would come before 08-02-2002. And of course your date format may be different. For that reason you may want to chose a Date object instead of a String object to represent the date and copare those in the compareTo method.
Michael Morris

Andrew Goh
Greenhorn
Posts: 4
Michael Morris ,
can ur example be used if there are 3 Strings instead of 2??
for example, in addition to date and name, there could be for example String comments.
regarding your question as to why "it begs the question why should the elements of the primary ArrayList be themselves ArrayLists if each of those lists only contains a Date and Name" i did that so that i could add in one more column containing String comments.

Michael Morris
Ranch Hand
Posts: 3451

can ur example be used if there are 3 Strings instead of 2??
for example, in addition to date and name, there could be for example String comments.

Sure thing.
Now, if you really want to use ArrayLists as elements, you can define a Comparator like this:

The results are the same, but it has the problem of needing to know how many elements (columns) are in the elemental ArrayLists. The consequences of that is that if you add a new column, you will undoubtedly need to change code in many places instead of just one.
Michael Morris

Andrew Goh
Greenhorn
Posts: 4
oh.. ok.. thanks for ur help... i will try it out.. thanks again