Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort list of string with localization in scala

i want to sort a list of strings. I know that's not difficult in scala but my problem is, that i need to sort lists in different languages. For example i know that i can sort strings in english very easily. But what's about the russian language or the romanian one? What is the best practice for sorting strings in multiple languages in scala? Does the scala sorting implementation support only english letters?

In java i would do something like this:

Collator coll = Collator.getInstance(locale);
coll.setStrength(Collator.PRIMARY)
Collections.sort(words, coll);

I hope someone out there can help me. Thanks in advance Nico.

like image 975
Nico Avatar asked Jul 21 '14 07:07

Nico


1 Answers

Nothing different here :). Collator is a comparable, so you convert it to a Ordering and then use it for sort.

scala> val ord = Ordering.comparatorToOrdering(Collator.getInstance(Locale.FRENCH));
ord: scala.math.Ordering[Object] = scala.math.LowPriorityOrderingImplicits$$anon$7@759fad4

scala> Seq("deux","Bonsoir","Merci").sorted(ord)
res13: Seq[String] = List(Bonsoir, deux, Merci)
like image 196
Jatin Avatar answered Oct 15 '22 20:10

Jatin