sortOn: sorts range of aggregates by member name(s)

via Digitalmars-d digitalmars-d at puremagic.com
Thu Nov 6 02:50:06 PST 2014


On Thursday, 6 November 2014 at 09:13:07 UTC, Nordlöw wrote:
> On Wednesday, 5 November 2014 at 21:29:20 UTC, Marc Schütz 
> wrote:
>> Again untested:
>>
>> private alias makePredicate(alias xtr) =
>>    (a, b) => (extractorFun!xtr(a) < extractorFun!xtr(b));
>
> This errors as
>
> sort_ex.d(35,43): Error: basic type expected, not (
> sort_ex.d(35,43): Error: function declaration without return 
> type. (Note that constructors are always named 'this')
> sort_ex.d(35,50): Error: semicolon expected to close alias 
> declaration
> sort_ex.d(35,50): Error: Declaration expected, not '=>'
>

It probably needs to be spelled out as function/delegate literal 
(sorry, don't have much time now).

>> auto sortBy(extractors..., R)(R r) {
>>    alias preds = staticMap!(makePredicate, extractors);
>>    return r.sort!preds;
>> }
>
> and this as
>
> sort_ex.d(37,6): Error: template sort_ex.sortBy(xtors..., R)(R 
> r) template tuple parameter must be last one

Try nesting the templates, then the tuple can come first:

     template sortBy(extractors...) {
         auto sortBy(R)(R r) {
             // ...
         }
     }

This should then be usable as `sortBy!("x", "y")(range)`.


More information about the Digitalmars-d mailing list