Java 排序与 MySQL 排序不同
Java sorting is not the same with MySQL sorting
我需要检查 table 中的排序,table 内容由 MySQL 给出。我正在尝试以下操作:
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
这就是我通过查询从 MySQL 得到的:
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 table 排序:
http://www.asciitable.com/
4 (52) - @ (64) - _ (95)
但在 MySQL 结果中顺序是 _ -> @ -> 4
email
字段排序规则是:utf8_unicode_ci
有什么问题,有没有比较器可以用同样的方式进行排序?
使用[Collator][1]
:
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);
我需要检查 table 中的排序,table 内容由 MySQL 给出。我正在尝试以下操作:
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
这就是我通过查询从 MySQL 得到的:
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 table 排序:
http://www.asciitable.com/
4 (52) - @ (64) - _ (95)
但在 MySQL 结果中顺序是 _ -> @ -> 4
email
字段排序规则是:utf8_unicode_ci
有什么问题,有没有比较器可以用同样的方式进行排序?
使用[Collator][1]
:
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);