This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
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://aspose.com/file-tools
 
subject: Ordering Lists of Lists (Table) by more than one column
 
Similar Threads
need suggestions on how to deal with data iteration problems
Iterating in a collection (LIST or MAP)
Single interval selection in two lists
extract particular elements from arraylist
Generics <? extends String> Question