This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Table sorting queries  RSS feed

 
Mark Dary
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H all

I have two table sorting queries.

First is how can I set the default sorting column before the table is first displayed.

And secondly, how can I implement multi-column sorting, for example on column 3, then column 2 then column 6 for example?

Any help appreciated.

Thanks.

Mark.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not 100% sure, but I think RowSorter.getSortKeys() will handle your second problem.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for #1

table.getRowSorter().toggleSortOrder( [columnIndex] );

will have the table sorted by the columnIndex when opened
 
shweta jha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for help
 
Mark Dary
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all

I got it working with your help, for example:


So now each time the table is refreshed (quite regularly with external data), the columns are automatically sorted in the order I want (3, 7, 2 for example).

My next query is how to implement user defined mulit-column sorting, eg Ctrl-click column 2, 6, 5 for example to sort by column 2, then 6 then 5?

Thanks again.

Mark.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DefaultRowSorter (which TableRowSorter extends) already implements this, albeit differently. If you toggle a sort order (by simply clicking on the header), that column will be placed first in the sort keys list. So to get your desired sort order, the user must click the columns in reverse order: 5, 6, 2.

This is how it works when toggleSortOrder is called (after pressing a column header):
- if the column is not sortable, abort
- if the column is already the first column, toggle the sort order
- otherwise move / add to the front in ascending order
- cut off any keys such that getSortKeys().size() <= getMaxSortKeys()
 
Mark Dary
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Sorry, but my original example is wrong, possibly a misunderstanding.

If I have the following data in 3 columns:



After sorting I want:



The example I tried earlier does not seem to work.

Any ideas?



 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!