Sort Associative Array by Key

Machine Code jckj33 at
Tue Aug 27 20:14:21 UTC 2019

On Tuesday, 27 August 2019 at 16:25:00 UTC, Samir wrote:
> On Sunday, 25 August 2019 at 17:01:23 UTC, a11e99z wrote:
>>> auto foo = ["VXE":8, "BZP":5, "JLC":2];
>>> foo.byPair.array.sort!"a[0]<b[0]".map!"a[1]".writeln;
> On Sunday, 25 August 2019 at 19:03:10 UTC, JN wrote:
>> I think normal lambdas are better than these string ones:
>> foo.byPair.array.sort!((a, b) => a[0] < b[0]).map!(a => 
>> a[1]).writeln;
> On Sunday, 25 August 2019 at 21:13:05 UTC, Paul Backus wrote:
>> You can also use names instead of numeric indices:
>> foo.byPair.array.sort!((a, b) => a.key < b.key).map!(a => 
>> a.value);
> a11e99z, JN, Paul:  Thank you all for your replies and help.  
> As I've mentioned on the list before, I really struggle to 
> understand how some of the std.algorithm functions such as 
> `map` work when combined with things like `array`, `sort` and 
> especially `zip` but really appreciate the support I find here 
> on the forum.
> Samir

It isn't really hard:
.array() turns the range into an array, so that sort() can work.
sort() takes "callback" function to test the elements while 
sorting and determine the proper position in the array.
map() select the property from each element, putting that 
requested property into an array.

More information about the Digitalmars-d-learn mailing list