How to sort data in table based on multiple Header Fields selected

Posted on 2002-06-20
Last Modified: 2010-03-31
I have to fill the JTable data with some date, time and string values. for example my table data looks like this:
"1998/12/14","15:14:39","Unicorn2","row2"         "1998/12/14","15:14:40","Unicorn4","row3"         "1998/12/17","12:14:12","Unicorn4","row6"                
Now the Sorted Table should be in the following way:
ie First Date field should be sorted, if 2 date fields are same then sort based on time. if date and time fields are same then need to be sorted on String field.

So if any one worked on this please theow some light on how to proceed. I know how to sort based on single column.
But now i need to sort a multiple columns.So what is code change in the Comparater class.

Thanks in advance.. This is urgent....
Question by:makineni_rk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 7095494
Is that data is coming from the database, you can sort it as you wish at the retrieving the data from the database.


Author Comment

ID: 7096072
Hi MaxMaxic,
The data is not coming from the database. Assuming it as static data

Expert Comment

ID: 7096223
You can try to sort first the least significant column and then go to next least significant column and etc'.
In your example it should be String field, time field, date field.

I hope it helps.
LVL 92

Accepted Solution

objects earned 100 total points
ID: 7097506
Just change your comparator to test if the two dates are equal, and if they are then perform the test on the second field etc.

if (d1.getTime()==d2.getTime())
   if (t1.getTime()==t2.getTime())
      return s1.compareTo(s2);
      return t1.getTime()-t2.getTime();
   return d1.getTime()-d2.getTime();
LVL 92

Expert Comment

ID: 7097906

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iterator/ListIterator approach 17 66
runtime exception 2 63
add projects t working set in maven 2 65
Which is best Image Resizing Web service 11 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question