Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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: 140
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: 43935
    
  33
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: 14950
    
  26

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 8 API documentation
 
Have you checked out Aspose?
 
subject: Case Insensitive Sort for Map Value
 
It's not a secret anymore!