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

via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 10 12:35:27 PST 2014


On Monday, 10 November 2014 at 03:38:49 UTC, Andrei Alexandrescu 
wrote:
> On 11/9/14 4:49 AM, "Nordlöw" wrote:
>> On Sunday, 9 November 2014 at 08:12:37 UTC, Andrei 
>> Alexandrescu wrote:
>>> sortBy!(expr) is equivalent with sort!(expr_a < expr_b) so 
>>> there's no
>>> additional power to it. However, it does make some sorting 
>>> criteria
>>> easier to write. A while ago I took a related diff pretty far 
>>> but in
>>> the end decided to discard it because it didn't add 
>>> sufficient value.
>>
>> What do you mean by sufficient value?
>
> It seemed to me just another way of doing the same things.
>
>> Ruby has sort_by. I believe D would when possible should be a 
>> superset
>> of the union of powers available in other languages to 
>> maximize the ease
>> of porting code *to* D and the pleasure for programmers to 
>> switch to D.
>
> Sadly things don't work that way - e.g. Phobos1 took a bunch of 
> string functions from Ruby and Python, to no notable effect.
>
>> I'll try to make this elegant and useful and use it myself for 
>> a while.
>> If it works for me I'll do a PR still. Ok?
>
> That's a better approach, thanks. A few compelling examples 
> would help.

A positive point about it would be that it's more intuitive to 
read, especially with long names or complex expressions.

     // sort vectors by length
     container.sortBy!"x*x + y*y";
     // vs.
     container.sort!((a,b) => a.x*a.x + a.y*a.y < b.x*b.x + 
b.y*b.y);


More information about the Digitalmars-d mailing list