java 忽略大小写排序,Java不区分大小写的本地化排序

I have set of hyphenated string sets. That I want to sort considering the locale.

List words = Arrays.asList("App - Small", "Apple", "App - Big");

Collator collator = Collator.getInstance(new Locale("en"));

// Sort Method 1

Collections.sort(words, String.CASE_INSENSITIVE_ORDER);

System.out.println(words.toString());

// Sort Method 2

collator.setStrength(Collator.PRIMARY);

Collections.sort(words, collator);

System.out.println(words.toString());

Result

String.CASE_INSENSITIVE_ORDER

[App - Big, App - Small, Apple]

Collator.PRIMARY

[App - Big, Apple, App - Small]

Though the Collator.PRIMARY is supposed to do a case-insensitive sorting there is difference between the order using the above two methods. How can I achieve locale based case-insensitive sort order that works with hyphen.

[App - Big, App - Small, Apple] - Expected sort order

解决方案

Below is the quote from the API.

It does not only depend on the strength but also other things. Enclose hyphen('-') between single quotes and you will get the desired output ''

The definitions of the rule elements is as follows:

Text-Argument: A text-argument is any sequence of characters, excluding special characters (that is, common whitespace characters [0009-000D, 0020] and rule syntax characters [0021-002F, 003A-0040, 005B-0060, 007B-007E]). If those characters are desired, you can put them in single quotes (e.g. ampersand => '&'). Note that unquoted white space characters are ignored; e.g. b c is treated as bc.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值