How to sort a range

cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 9 06:28:11 PST 2016


On Wednesday, 9 March 2016 at 12:21:55 UTC, rcorre wrote:
> On Wednesday, 9 March 2016 at 09:15:01 UTC, Edwin van Leeuwen 
> wrote:
>>
>> I'm not sure why your fix didn't work, but generally I work 
>> around this by converting the OnlyResult into an array:
>>
>> import std.array : array;
>> assert(only(3,1,2).array.sort.equal(only(1,2,3)));
>
> I'd like to avoid allocating here.

Note that an input range isn't even remotely a container, it's a 
way to iterate on a container. As you don't have all elements at 
hand you can't sort them, that's why you have to use array here. 
If you don't want to allocate using the GC just allocate your own 
memory and store your range elements in it before sorting but 
sort has to have access to all elements one way or another.


More information about the Digitalmars-d-learn mailing list