# Sorting strings by Unicode Collation order?

**URL:** https://discourse.julialang.org/t/sorting-strings-by-unicode-collation-order/11195
**Category:** Internals & Design
**Tags:** strings, sort, unicode
**Created:** [May 28, 2018, 6:48am UTC](https://discourse.julialang.org/t/sorting-strings-by-unicode-collation-order/11195 "2018-05-28T06:48:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [May 28, 2018, 6:48am UTC](https://discourse.julialang.org/t/sorting-strings-by-unicode-collation-order/11195/1 "2018-05-28T06:48:34Z")

</div>

Is there a way to sort strings by the Unicode specified Collation order in Julia? Currently, a plain `sort` seems to sort just by codepoint order, for eg.

```julia

julia> current_order = sort(["மனம்", "மயம்", "மகம்", "மறம்", "மலம்"])
5-element Array{String,1}:
 "மகம்"
 "மனம்"
 "மயம்"
 "மறம்"
 "மலம்"

julia> expected_order = ["மகம்", "மயம்", "மலம்", "மறம்", "மனம்"]
5-element Array{String,1}:
 "மகம்"
 "மயம்"
 "மலம்"
 "மறம்"
 "மனம்"

julia> current_order .== expected_order
5-element BitArray{1}:
  true
 false
 false
  true
 false

```

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [May 28, 2018, 12:12pm UTC](https://discourse.julialang.org/t/sorting-strings-by-unicode-collation-order/11195/2 "2018-05-28T12:12:28Z")

</div>

I looked a bit into what a few other major languages do regarding Collation.

Python apparently leaves it to external libraries (eg: pyuca), Ruby too (ICU, TwitterCLDR, etc.).

Perl’s `sort` [uses](http://perldoc.perl.org/perllocale.html) collation algorithm when `use locale` is set to a UTF-8 locale.

Java [does collation](https://docs.oracle.com/javase/tutorial/i18n/text/locale.html) with the `Collator` class, with the locale specified in the constructor.

C# [has](https://docs.microsoft.com/en-us/globalization/locale/sorting-and-string-comparison) `Globalization.SortKey` that uses the collation algorithm depending on the `CurrentCulture` (locale) setting.

It makes sense for the default `sort` to do it the simple, efficient, and locale-independent way (the current comparator seems to boil down to `c = ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, min(al,bl))` in `cmp(::String, ::String)`). But it would be useful to have the option of using a sort that uses a collation algorithm, perhaps as an overload in the stdlib `Unicode` module.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [May 28, 2018, 12:13pm UTC](https://discourse.julialang.org/t/sorting-strings-by-unicode-collation-order/11195/3 "2018-05-28T12:13:06Z")

</div>

> [@digital\_carver](#):
>
> Is there a way to sort strings by the Unicode specified Collation order in Julia?

There are all sorts of collation orders, what in particular were you expecting?  
Generally, anything more complex than sorting normalized strings by the Unicode codepoints needs to be locale specific (French in particular has very complicated rules for what they consider correct for dictionaries and phone books).

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [May 30, 2018, 5:20pm UTC](https://discourse.julialang.org/t/sorting-strings-by-unicode-collation-order/11195/4 "2018-05-30T17:20:06Z")

</div>

Collation is a very complex task, with many different collations used even within a single locale.  
Probably the best thing currently available in Julia would be to use `ICU` or `StrICU` (as soon as that’s registered, hopefully soon), as you noted, other languages use the ICU library, while large, it’s pretty much the gold standard for dealing with complex Unicode issues.
