Sorting with non-ASCII characters

Jos van Uden usenet at fwend.com
Thu Sep 19 11:44:53 PDT 2013


On 19-9-2013 17:18, Chris wrote:
> Short question in case anyone knows the answer straight away:
>
> How do I sort text so that non-ascii characters like "á" are treated in the same way as "a"?
>
> Now I'm getting this:
>
> [wow, ara, ába, marca]
>
> ===> sort(listAbove);
>
> [ara, marca, wow, ába]
>
> I'd like to get:
>
> [ ába, ara, marca, wow]

If you only need to process extended ascii, then you could perhaps
make do with a transliterated sort, something like:

import std.stdio, std.string, std.algorithm, std.uni;

void main() {
     auto sa = ["wow", "ara", "ába", "Marca"];
     writeln(sa);
     trSort(sa);
     writeln(sa);
}

void trSort(C, alias less = "a < b")(C[] arr) {
     static dstring c1 = "àáâãäåçèéêëìíîïñòóôõöøùúûüýÿ";
     static dstring c2 = "aaaaaaceeeeiiiinoooooouuuuyy";
     schwartzSort!(a => tr(toLower(a), c1, c2), less)(arr);
}



More information about the Digitalmars-d-learn mailing list