java对mysql排序_Java排序与MySQL排序不同

bd96500e110b49cbb3cd949968f18be7.png

I need to check the sorting in the table and table content is given by MySQL. I'm using the following row to sort the list:

Collections.sort(sorted, String.CASE_INSENSITIVE_ORDER);

And get the following result:

tes3@test.com

test4@test.com

test5@test.com

test@test.com

test_user@mail.com

user-og@driver.com

And this is what I get from MySQL by query:

SELECT 'email' FROM 'user' WHERE 1 ORDER BY 'user'.'email' ASC :

tes3@test.com

test_user@mail.com

test@test.com

test4@test.com

test5@test.com

user-og@driver.com

As you can see, the order is not the same.

Seems that Java sorts according to ASCII table:

http://www.asciitable.com/

4 (52) - @ (64) - _ (95)

But in MySQL result the order is _ -> @ -> 4

The email field collation is: utf8_unicode_ci

What is the problem and is there any comparator to make ordering in the same way?

解决方案

Use [Collator][1]:

The Collator class performs locale-sensitive String comparison. You use this class to build searching and sorting routines for natural language text.

And code will be:

Collator coll = Collator.getInstance(Locale.US);

coll.setStrength(Collator.IDENTICAL);

Collections.sort(words, coll);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值