File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparators sorting ascending and descending differently ..

 
Manju Krishna
Ranch Hand
Posts: 48
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manju Krishna
Ranch Hand
Posts: 48
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help...
It works as i wanted it to..

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic