Sort list of string with localization in scala

巧了我就是萌 提交于 2019-12-09 15:45:05

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/24860138/sort-list-of-string-with-localization-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!