File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Sorting Complication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Sorting Complication" Watch "Sorting Complication" New topic

Sorting Complication

Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Hi All,
I wanted to implement a simple sorting for the JTable (I have cursed sun at least 1,000,000 times for not providing that)

I implemented a simple single level sorting taking help from the TableSorter from the Examples. But there is a small complication.

when there are many rows with same values i just leave the order as it is. But now if i sort it based on column 1 first and then go to column 2 then the order is different and if i go to column 3 first and then to column 2 then t order is different

Please let me know if that is an issue?

-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The abililty to sort a collection while leaving equal items in the same order in which they originally appeared is called stability. A stable sort is one that wouldn't have the problem that you describe. The java.util.Arrays.sort(Object[]) method is a stable quicksort -- you could use that.

[Jess in Action][AskingGoodQuestions]
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Thanks Ernst,
I too have done something similar to what you are telling. But the problem that i am facing i will explain
Suppose there are three rows (simplified though)

So you see that after the step 2 of both the variations even though i am sorting the column 2 and it indeed is sorted but yet the rows with the same City will not be having the same order as it depends on which column was selected earlier.

I was wanting to know if that is fine as long as sorting is done correctly on the column 2
I agree. Here's the link:
subject: Sorting Complication
It's not a secret anymore!