jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Sorting Table Columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting Table Columns" Watch "Sorting Table Columns" New topic

Sorting Table Columns

amrit choong

Joined: Dec 18, 2004
Posts: 24
Hi Friends,
I've a table with the following fields and infinite rows of the type given below:
Rollno FirstName LastName
int string string

I am not using JTable,I am creating a HTML table from the resultset.
I want that when i click on any of the headers(eg rollno,firstname,lastname) all the other rows should be sorted.
Can someone tell me how to do that or refer me some examples?
I want the exact results as shown in the examples below,but the problem is that they are in swing,and i can't use swing
and neither do i understand much of it.

Any help wud be appreciated,thank you
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
I just have some idea though not fully worked on it. I would suggest you

- Create as many java.util.TreeSet objects as there are no. of columns/fields in ResultSet, to represent each column in html table.
Also different Comparator classes for each field to use for sorting.

TreeSet rollno = new TreeSet(new yourownComparator());
TreeSet firstname = new TreeSet();
TreeSet lastname = new TreeSet();

- to store each row field in these TreeSet objects use HashMap;
HashMap hrollno;
HashMap hfirstname;

This way you will get first sorted column in roll but to allign other unsorted columns in other TreeSet objects which are created without Comparator( because sort is done only on one column at a time). you can use other TreeSet object individual HashMap values to determine in which row to show that value.

- in each Comparator class of your own, you will override those equal and compare methods to specify your own logic i.e. each object is HashMap and you will compare the HashMap value not the keys which represents the row no.

I assume you will use Applet to show tables and the whole ResultSet values are transfered to client side in TreeSet Objects
for later reshuffling/sorting on other columns.

Hope this works.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

How will the results be viewed?

Since Damanjit assumed you will use an applet, I will assume you are using a web browser to view the results.

In the past, I have done the sorting thing on a web page by having each column header be a link that calls the same page with the argument containing the column name. The query associated with that jsp page is adjusted to ORDER BY whatever column the user has clicked.

There are several client side tools to do the same thing without having to submit back to the server. You will have to test these to see if they can handle a dataset that is "infinite".

webfx is the client side sorting tool I have seen working well:
[ December 30, 2004: Message edited by: Tim McGuire ]
amrit choong

Joined: Dec 18, 2004
Posts: 24
Thanks damanjit and Tim,
I am trying to do exactly wat Tim has stated.I am trying to get the same thing working.Let me try those tools and get back to u asap.
Thanks for the help.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
The open source FormattedDataSet API does sorting of data from a result set and generates html table results.

In fact the live demo allows you to enter a query which is executed and click on the column header to sort. The formatteddataset link is below. The sortable query live demo follows.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
amrit choong

Joined: Dec 18, 2004
Posts: 24
Thanks a lot for your help.I went thru the code at www.fdsapi.com,and instead of using that api,I'm trying to make it go my way.Can anyone please help me explain how these two comparators(used to sort a Object[][]) work:

I want to make two changes in this comparators:
I want the sorting to be done in CASE_INSENSITIVE_ORDER(eg AaBb.....Zz and not AB...Zab...z),and Secondly I want to sort the numbers(like Integers)as Numbers,while its presently being done taking numbers as strings,which I dont want.
please help me..waitin for ur replies friends.
I agree. Here's the link: http://aspose.com/file-tools
subject: Sorting Table Columns
jQuery in Action, 3rd edition