Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Daslan Govender
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic