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

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

问题描述

我想按照用户区域设置对字符串列表进行排序。

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参数传递给

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

推荐答案

您可以使用排序与自定义比较器。请参见 Collat​​or界面

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);

整理者是一个比较器,可以直接传递到 Collections.sort (...)方法。

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

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

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