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

JTable Sort Numeric Column  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to sort a JTable on an all numeric (integer) column. From what I read, if the sorter knows the type is integer it should sort correctly. This is my code:

The result is when I sort on that column (0), it sorts alphabetically not numerically. What am I doing wrong? TIA.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard to tell what you are doing wrong because you don't show the code where you tell Swing how to sort your table. Here's a fragment from my code which results in a JTable with correctly-sorting numeric (and date) columns:


 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:The result is when I sort on that column (0), it sorts alphabetically not numerically. What am I doing wrong?


By any chance is your getValueAt() method returning the result of calling toString() on the Integer? Just change it to return the Integer value for the cell instead of its string representation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!