*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38025
    
  22
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: 14074
    
  16

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Case Insensitive Sort for Map Value
 
Similar Threads
(Restrictions.eq("name",name)) - Case Insensitive?
How to compare each individual char of 2 string
Tag question
String program to count the repetitive words in a string.
is the search case sensitive?