How to sort a range

rcorre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 9 07:39:55 PST 2016


On Wednesday, 9 March 2016 at 14:28:11 UTC, cym13 wrote:
> 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.

In the general case, yes. However only is a range wrapper around 
a static array, and does have all elements at hand. Maybe I 
should just be using a static array...

Still curious as to why it fails; maybe the range is getting 
copied at some point? I guess I need to step through it.

I did try different SwapStrategies with no luck.


More information about the Digitalmars-d-learn mailing list