This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sorting a table with x cols Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting a table with x cols" Watch "Sorting a table with x cols" New topic
Author

Sorting a table with x cols

Amit Chandak
Greenhorn

Joined: Sep 28, 2003
Posts: 9
What is the best data structure in java to represent a table with x cols.
And is there a simple way to sort the table on any one of the cols?
Thanks
Amit
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Originally posted by Amit Chandak:
What is the best data structure in java to represent a table with x cols.

Like all things programming, it depends. javax.swing.table.DefaultTableModel is a good place to start.


And is there a simple way to sort the table on any one of the cols?
Thanks
Amit

javax.swing.table.DefaultTableModel can be set up to use 2d arrays of Objects, and the java.util.Arrays object has lots of sort functionality. . .


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Originally posted by Amit Chandak:
What is the best data structure in java to represent a table with x cols.
And is there a simple way to sort the table on any one of the cols?
Thanks
Amit

Try using SortedSet and implementing Comparable interfaces for the objects used to compare.
Regards,
Vinod.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'd favor Collections.sort() rather than a SortedSet, as rows may or may not contain duplicate values; we don't necessarily care. And it sounds like you may want to sort on different columns. If you sort on column 1 and then on column 2, the column 1 sort is probably no longer valid; if you're holding on to a SortedSet representing that, you may well be in for confusion. Inside my TableModel I'd have a single List representing all the rows in their current order, and when you want to sort on a particular column, call Collections.sort(List, Comparator) using a Comparator that compares the particular column you wish to sort. Once you sort on column 2, the sort by column 1 is abandoned (unless/until you re-sort it by column 1 again). There are other wayt to do this, simpultaneously maintaining multiple TreeMaps for the various sort orders you may wish to deal with, but this will be somewhat more complex.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting a table with x cols
 
Similar Threads
Making a buffered image of a JTable row
write to text file in tabular form
adding attribute to existing tag. How???
Showing database rows as columns
question on entity bean.