.sort vs sort(): std.algorithm not up to the task?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 7 19:39:35 PDT 2017


On Thursday, June 08, 2017 02:31:43 Stanislav Blinov via Digitalmars-d-learn 
wrote:
> On Thursday, 8 June 2017 at 02:25:17 UTC, Jonathan M Davis wrote:
>
> Oh I see, the was error related to iteration, not sorting.
>
> > Ranges do not support iterating with an index. The workaround
> > if you want to have an index with ranges and foreach, then you
> > should use lockstep:
> >
> > http://dlang.org/phobos/std_range.html#lockstep
> >
> > e.g.
> >
> > foreach(i, v; lockstep(iota!size_t(0), s))
> > {}
> >
> > or
> >
> > foreach(i, v; lockstep(iota(0), s))
> > {}
>
> There's an enumerate():
> https://dlang.org/phobos/std_range.html#enumerate
>
> import std.algorithm : sort;
> import std.range : enumerate;
>
> foreach(i, k; aa.keys.sort().enumerate) {
>      /* ... */
> }

Even better. I hadn't realized that such a function had been added.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list