• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Case Insensitive Sort for Map Value

 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49817
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15445
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic