This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Comparators sorting ascending and descending differently .. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparators sorting ascending and descending differently .. " Watch "Comparators sorting ascending and descending differently .. " New topic
Author

Comparators sorting ascending and descending differently ..

Manju Krishna
Ranch Hand

Joined: Apr 09, 2009
Posts: 48
    
    1
Hi..

I wrote a comparator to compare to strings... the ascending seems to be working fine..
but the result of desending sort is not just the exact reverse of ascending order.


the code snippet...



Please help..

result after ascending sort..
1234567
QAAe MEDu
QAae MEnu
qaAe Menu
QADe Menu
QBCC Menu
qbcc Menu


result after descending sort..
QBCC Menu
qbcc Menu
QADe Menu
QAae MEnu
qaAe Menu
QAAe MEDu
1234567
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Please Use Code Tags. It preserves your indentation, making the code easier to read. I've added them for you this time.


The sort order is correct though; you are comparing them ignoring the case, so "QBCC Menu" and "qbcc Menu" are the same. That means, the sorting algorithm can position them any way it wants (but usually the first one encountered comes first).

You could change your option 5 as follows:
This way, if they are equal when you ignore the case, you include the case in the comparison.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Manju Krishna
Ranch Hand

Joined: Apr 09, 2009
Posts: 48
    
    1
Thanks for the help...
It works as i wanted it to..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparators sorting ascending and descending differently ..
 
Similar Threads
JTable Dynamic - Urgent
Color in JTable
Refresh JTable with New data
Sorting the Rows in a JTable Component Based on a Column
JTABLE getValueAt compiling error