jQuery in Action, 2nd edition*
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 Android Security Essentials Live Lessons this week in the Android 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: 19655
    
  18

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
Color in JTable
JTABLE getValueAt compiling error
JTable Dynamic - Urgent
Sorting the Rows in a JTable Component Based on a Column
Refresh JTable with New data