File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Case Insensitive Sort for Map Value

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 151

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

Joined: Oct 13, 2005
Posts: 46348
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: 15081

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
I agree. Here's the link:
subject: Case Insensitive Sort for Map Value
It's not a secret anymore!