• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Tree Map Sort & foreign languages

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a tree map that I want to sort on the key. Works fine as long as you use english. When you get into other languages that have accents the sort does not work correctly. In java script there is a call you can use for a more natural search called localeCompare.

Is there a way in java to make a TreeMap have a more natural search?



sortedMap returns btest, ctest, ztest in the correct order. Now if you add a character like optList.put("ét", "1") to the hashmap ét and run it through the tree map it always returns it at the end of the list instead of between ctest & ztest key.

Can a TreeMap do a natural sort that will understand that order of the keys returned should be btest, ctest, ét, ztest?

Thanks
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, because you can create a TreeMap which uses a Comparator which you define.

If you check out the API documentation for Comparator you will see that one of the standard classes which implements it is Collator. So for example if you wanted to collate strings according to the rules for French (as per your example), you could use

to make that happen. Like this:
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant thank you!
 
no wonder he is so sad, he hasn't seen this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic