Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does STL sort support UTF8?

Does the STL sort function support alphabetical sorting of names which have UTF-8 characters in them? Say names from German/French language?

like image 976
Spottsworth Avatar asked Aug 28 '26 04:08

Spottsworth


2 Answers

That entirely depends on how you store the UTF-8 characters and how your comparer looks like. The sort function is completely agnostic of the elements it sorts.

But you probably mean “… when stored in a char array” and then the answer is no since the chars will store individual bytes of a given UTF-8 character, instead of the logical character. The sort function sorts elements delimited by iterators. sort works only if the iterators / the elements they refer to are aware of the data that they contain. This isn’t the case for an array of chars that encode UTF-8.

The “correct” solution here is to parse the UTF-8 input into an array of proper (normalised) Unicode code points, sort those, and translate back to UTF-8.

like image 115
Konrad Rudolph Avatar answered Aug 30 '26 19:08

Konrad Rudolph


All that is required is the proper comparison function. You can probably find one in ICU - International Components for Unicode . Look specifically at Collation.

like image 31
Mark Ransom Avatar answered Aug 30 '26 19:08

Mark Ransom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!