Java排序与MySQL排序不同 [英] Java sorting is not the same with MySQL sorting

查看:263
本文介绍了Java排序与MySQL排序不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查表中的排序,表的内容由MySQL提供。我使用以下行对列表排序:

Collections.sort(sorted,String.CASE_INSENSITIVE_ORDER);

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

并获得以下结果:

tes3@test.com

test4@test.com

test5@test.com

test@test.com

test_user@mail.com

user-og@driver.com

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

这是我通过查询从MySQL获得的:

SELECT'email'FROM'user'WHERE 1 ORDER BY'user'。'email'ASC

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

正如你所看到的,顺序是不一样的。
看来Java根据ASCII表排序:
http://www.asciitable.com/
4(52) - @(64) - _(95)

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)

在MySQL结果中,顺序是 _ - > @ - > 4

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

电子邮件字段排序规则为: utf8_unicode_ci

有什么问题,是否有任何比较器以同样的方式进行排序?

The email field collation is: utf8_unicode_ci
What is the problem and is there any comparator to make ordering in the same way?

推荐答案

使用 [Collat​​or] [1]


Collat​​or类执行区域设置敏感的字符串比较。您可以使用此类为自然语言文本构建搜索和排序例程。

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

代码如下:

    Collator coll = Collator.getInstance(Locale.US);
    coll.setStrength(Collator.IDENTICAL); 
    Collections.sort(words, coll);

这篇关于Java排序与MySQL排序不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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