aspose file tools*
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
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,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ordering Lists of Lists (Table) by more than one column