aspose file tools*
The moose likes Java in General and the fly likes Case Insensitive Sort for Map Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Case Insensitive Sort for Map Value" Watch "Case Insensitive Sort for Map Value" New topic
Author

Case Insensitive Sort for Map Value

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
Hi!

I want to sort the values of a Map and it should be case insensitive for string.

I have searched the web and the closest one is by paaloliver but that is not case-insensitive.

Can anyone help with case-insensitive sort?

Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
I presume you are using something like a TreeMap, which can take a Comparator. There is a Comparator which is a field of the String class, which is intended for case-insensitive sorting.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14149
    
  18

It's not very hard to modify that example to make the sort case-insensitive. You'd have to change something in the compare() method of class ValueComparer, but you'd also have to change it so that type V is always String (because case sensitivity only makes sense for String objects; comparing arbitrary objects case insensitive makes no sense...).

@Campbell: But that can only used to sort the map by keys, not by values.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Case Insensitive Sort for Map Value