Solved

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

Posted on 2002-06-20
5
169 Views
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:38","Unicorn1","row1"
"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:
"1998/12/17","12:14:12","Unicorn4","row6"                
"1998/12/14","15:14:40","Unicorn4","row3"                
"1998/12/14","15:14:39","Unicorn2","row2"                
"1998/12/14","15:14:38","Unicorn1","row1"
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....
0
Comment
Question by:makineni_rk
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:MaxMaxic
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.

Regards.
0
 

Author Comment

by:makineni_rk
ID: 7096072
Hi MaxMaxic,
The data is not coming from the database. Assuming it as static data
rds,
ram
0
 
LVL 1

Expert Comment

by:MaxMaxic
ID: 7096223
Hi.
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.
0
 
LVL 92

Accepted Solution

by:
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);
   }
   else
   {
      return t1.getTime()-t2.getTime();
   }
}
else
{
   return d1.getTime()-d2.getTime();
}
0
 
LVL 92

Expert Comment

by:objects
ID: 7097906
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split string containing \r\n in Java 46 43
map related example 6 47
type mismatch (Object[] to double[] 4 24
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 32
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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