This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Ordering Lists of Lists (Table) by more than one column Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Ordering Lists of Lists (Table) by more than one column" Watch "Ordering Lists of Lists (Table) by more than one column" New topic
Author

Ordering Lists of Lists (Table) by more than one column

Daslan Govender
Greenhorn

Joined: Jan 14, 2005
Posts: 19
Hi
I have a struture which is an ArrayList of String[]. I wish to sort
the String[0], String[1], etc.

Any Ideas?

Tks in Advance,
d
Robert Waals
Greenhorn

Joined: Dec 22, 2006
Posts: 14
The convenient way would (if your data comes from a database) to do the sorting in the SQL statement.

Otherwise check this site:
http://www.javaworld.com/ sorting
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
An arraylist can be sorted by using the method

Collections.sort(ArraytoSort)
This wil sort the arraylist of strings in their natural order.

If you want to sort your own objects in JAVA you have to define your own Comparable interface which can sort over different columns because you have to define your own CompareTo method.

If you have different ways of sorting the best way is to define a Comparator so you can do the sorting with different Comparators.

Collections.sort(ArrayToSort, Comparator)

If the data retrieval is from one table of the database you can choose to do a data retrieval with each new sort order. This would be a performance issue if the dataretrieval is complex and you have to go everytime to the database.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Ordering Lists of Lists (Table) by more than one column
 
Similar Threads
Generics <? extends String> Question
need suggestions on how to deal with data iteration problems
Single interval selection in two lists
Iterating in a collection (LIST or MAP)
extract particular elements from arraylist