Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sorting Formatted Numbers In JTable

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Sorting Formatted Numbers In JTable

    Ok, well I have a JTable with several different types of text in each column and I would like to know how to specify how to sort each column. Some of the text is just text, some are numbers, and some are formatted numbers. At the moment, it is obviously treating each column as Strings, so it is just sorting based on the text-equivalent of each cell of data.

    So, here is the outline of the cells, 2 examples of their data, and how I want to sort them (column numbers are in indexes):

    Column 2:
    Type: Double, only positive numbers
    Example 1: 150
    Example 2: 1,000.25
    Sort Goal: Numerically

    Column 3:
    Type: String containing a double and an int, only positive numbers
    Example 1: 1,098/1,098
    Example 2: 29.15/150
    Sort Goal: Numerically by the number after the slash

    Column 4:
    Type: String, representing a percentage, only positive percentage
    Example 1: 100%
    Example 2: 15%
    Sort Goal: Numerically

    Column 5:
    Type: Double, can be negative or positive
    Example 1: 27,110.13
    Example 2: -289.74
    Sort Goal: Numerically

    Here is my code of giving the ability to sort:
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);

    I've been trying to do several things, but I just don't understand enough about how the TableRowSorter works in order for me to break some ground here. Can anyone give me some guidance as to how to specify how I want the rows to be sorted?

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  2. #2
    Join Date
    Aug 2011
    My Mood
    Thanked 4 Times in 4 Posts

    Default Re: Sorting Formatted Numbers In JTable

    You need to override the method getColumnClass() in your table model class (extends from AbstractTableModel).
    This method returns Class type for each column, so the TableRowSorter will sort columns accordingly.

    The following example will make the table sorts the 1st column as number (Integer.class) and other columns as String (String.class)

        public Class getColumnClass(int column) {
            if (column == 1) {
                return Integer.class;
            } else {
                return String.class;

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:27 PM. Reason: corrected the code

Similar Threads

  1. Writing formatted data to file
    By kafka82 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 20th, 2011, 03:07 PM
  2. Creating a formatted printable report
    By bradleysm in forum Java Theory & Questions
    Replies: 6
    Last Post: January 27th, 2011, 04:46 PM
  3. How to get unix timestamp from formatted date?
    By snytkine in forum Java Theory & Questions
    Replies: 5
    Last Post: October 12th, 2010, 08:07 AM
  4. Help with sorting numbers
    By planktonx in forum Collections and Generics
    Replies: 2
    Last Post: October 5th, 2010, 08:01 AM
  5. Swing JTable sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 30th, 2010, 08:51 AM