Rust sort and locale

When I sorted a vector of slices I was baffled by the result:

let mut v = [ "zoo", "a dog", "Zoology", "Anton" ];

v.sort();
println!("sorted: {:?}", v);
sorted: ["Anton", "Zoology", "a dog", "zoo"]

I had assumed

sorted: ["a dog", "Anton", "zoo", "Zoology"]

like the sort command would do.

Then I discovered why things are like this.

The sort commands respects the locale settings whereas the Rust sort does not.
So, if I run: LC_ALL=C sort testfile the result is the same as in Rust sort.

Question: How do I tell the Rust sort to respect the locale settings?

You need to provide your own comparator for locale-aware sort.

fn locale_cmp(left: &&str, right: &&str) -> std::cmp::Ordering {
    ...
}

fn main() {
    ...
    v.sort_by(locale_cmp);
}

Thanks.

In the meantime I read that Rust’s stdlib does not deal with locale stuff.

I have to admit that I believe it was a good decision not to introduce locale things in the stdlib. Thinking about it I believe that sorting, and respecting locale, could be pretty complicated.

Fortunately, in my use case the Rust way of sorting (LC_ALL=C) is ok for me. But I wanted to understand.