Sorting according to a primary and secondary criterion

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Wed Jul 17 05:33:45 PDT 2013


On 07/17/2013 02:07 PM, bearophile wrote:
> Another way is to use something like this, but this needs some memory:
> 
> idx.schwartzSort!(i => tuple(arr1[i], arr2[i]));

Oh, nice thought! :-)

> But often the most efficient way is to use sort() with a comparison function
> that takes in account all your sorting criteria.

That's what I assumed, but I don't understand how to provide that criterion to
sort: if I do e.g.

    idx.sort!("arr1[a] < arr1[b] || (arr1[a] == arr1[b] && arr2[a] < arr2[b])");

I (unsurprisingly) get a load of errors about std.functional not having access
to arr1 or arr2.

Contrast with the example in std.algorithm docs:

    words.sort!("toUpper(a) < toUpper(b)", SwapStrategy.stable);

... but of course toUpper is a publicly available function.


More information about the Digitalmars-d-learn mailing list