Iterate/sort associative array by value?

Seb seb at wilzba.ch
Mon Apr 8 21:09:29 UTC 2019


On Monday, 8 April 2019 at 18:04:28 UTC, kdevel wrote:
> On Sunday, 7 April 2019 at 17:16:12 UTC, Seb wrote:
>> ---
>> ["a": 1].byPair.array.sort!((a, b) => a.value < 
>> a.value).release.each!writeln;
>> ---
>
> What's the purpose of .release? The documentation in 
> https://dlang.org/phobos/std_range.html#.SortedRange.release is 
> rather monosyllabic.

As others have already explained, you'll get the original range 
back (instead of the SortedRange).
Sometimes, this is useful.
The obvious example is assigning back to the original range:

---
import std.experimental.all;
void main()
{
     auto arr = ["a": 1].byPair.array;
     arr = arr.sort!((a, b) => a.value < a.value);
}
---

https://run.dlang.io/is/8sFxVb

OTOH this works:

---
import std.experimental.all;
void main()
{
     auto arr = ["a": 1].byPair.array;
     arr = arr.sort!((a, b) => a.value < a.value).release;
}
---
https://run.dlang.io/is/TgXUZj

In the example where I used it, you won't need it.
Sorry for the confusion, but as it's often every now and then 
pretty useful, I thought it is a nice idea to give it more 
spotlight as it's one of the lesser known parts of Phobos.


More information about the Digitalmars-d-learn mailing list