• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting in TreeMap

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers
I am using TreeMap so that all my data will be sorted on the basis of keys. It work fine for me except that it sorts all the data based on the upper case and lower case . For e.g. If my keys are A,c,E,D,b the sorted output will be A,D,E,b,c . However I dont want to sort the data based on the case sensitivity. The desire output for me is A,b,c,D,E . Is there any collection which can gimme this kinda sorting. Using comparator is one of the option . But I am just trying to explore something apart from comparator.
Thanks
Samir
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to use TreeMap and customize the order, then you have two options:

  • Write your own Comparator and pass it to the constructor of TreeMap.
  • Make the class that you use for the keys implement interface Comparable, and add the appropriate compareTo() method to your key class.


  • If you are using normal Strings as the keys, then the second isn't going to work, because you can't change the implementation of class String.
     
    Sheriff
    Posts: 22849
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Try using String.CASE_INSENSITIVE_ORDER as the Comparator.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic