使用本地化对字符串列表进行排序 [英] Sort List of Strings with Localization

查看:37
本文介绍了使用本地化对字符串列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户区域设置在字符串列表下方进行排序

I want to sort below List of strings as per user locale

List<String> words = Arrays.asList(
      "Äbc", "äbc", "Àbc", "àbc", "Abc", "abc", "ABC"
    );

对于不同的用户语言环境,排序输出应该根据语言环境不同.

For different user locale sort output should be different as per there locale.

如何根据用户区域设置对上面的列表进行排序?

How to sort above list as per user locale ?

我试过了

Collections.sort(words , String.CASE_INSENSITIVE_ORDER);

但这不适用于本地化,那么如何将 locale 参数传递给 Collections.sort() 或者还有其他有效的方法吗?

But this is not working for localization, so how to pass locale parameter to Collections.sort() or is there any other efficient way ?

推荐答案

您可以将排序与自定义比较器一起使用.请参阅整理器界面

You can use a sort with a custom Comparator. See the Collator interface

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

collat​​or 是一个比较器,可以直接传递给 Collections.sort(...) 方法.

The collator is a comparator and can be passed directly to the Collections.sort(...) method.

这篇关于使用本地化对字符串列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆