This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Use Comparator to sort more than one column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use Comparator to sort more than one column" Watch "Use Comparator to sort more than one column" New topic
Author

Use Comparator to sort more than one column

Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
I'm using comparator interface to sort list of object.
Problem comes when there are more than one column to sort. For example :
I've unsorted tables :
Id|Department|Sub Department
1 Test Sub Test
2 Abap Sub Abap2
3 Abap Sub Abap1

How do i have two sort, Department then sub department. It become :
Id|Department|Sub Department
3 Abap Sub Abap1
2 Abap Sub Abap2
1 Test Sub Test

I'm implementing Comparator interface :


but it doesn't worked. Any idea ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It's more like



You compare using the second key only of the first keys are equal.


[Jess in Action][AskingGoodQuestions]
Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
thanks, you save me a lot time.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Use Comparator to sort more than one column
 
Similar Threads
Sorting a table of ArrayLists
How to "flip" an array?
Sorting the Rows in a JTable Component Based on a Column
vector sorting
Sorting JTable with scientific notation doubles in Win32