Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql order by on column with unicode characters

I am running a select query on mysql table and trying to order it by the "name" column in the table.
The name column contains both English character names and names with Latin character like â.
I am running into the below problem.
The query I run returns the results ordered in the below manner i.e.
Eg: if Name contains "archer", "aaakash", "â hayden", "bourne", "jason"
The results returned by the query is ordered as below
"aaakash", "archer", "â hayden", "bourne", "jason"

However I want to order it based on unicode code points (like below)
"aaakash", "archer", "bourne", "jason", "â hayden"

(See the difference in the position of â hayden in the orders)
What can I do to order the results based on the character's position in unicode character set?

like image 874
Bourne Avatar asked Jan 13 '23 15:01

Bourne


1 Answers

However I want to order it based on unicode code points (like below)

To sort using unicode code point, you probably need to use utf8_bin collation.

Precisely, the _bin suffix indicate to sort by the binary representation of each character.


To override the default collation while ordering, you will use ORDER BY ... COLLATE:

To paraphrase the documentation:

SELECT k
FROM t1
ORDER BY k COLLATE utf8_bin;

If your text column does not use utf8 encoding, you will have to CONVERT it:

SELECT k
FROM t1
ORDER BY CONVERT(k USING utf8) COLLATE utf8_bin;

Please notice I used utf8 as an example here as this is the most common Unicode encoding. But your MySQL server probably support other Unicode encoding, like ucs2("UTF-16").

like image 107
Sylvain Leroux Avatar answered Jan 19 '23 14:01

Sylvain Leroux