sorting std.container

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 11 12:06:33 PDT 2016


On Monday, 11 July 2016 at 18:54:44 UTC, George M wrote:
> Hello everybody,
>
> sorry for my bad english(nativ german). D is a very nice 
> language and easy to learn because i am a c# (only private) 
> developer.
> The only bad is to find examples is very hard.
>
> My question:
> How can i sort a slist or dlist with custom types and lambda 
> expressions?
>
> The sort function from std.algorithm works only with arrays (in 
> my test).
>
> Can give anyone an short example?
>
> Thanks for your help.
>
> George

The "tipical" sorting functions (as those in std.algorithm) 
expect their input to be a RandomAccessRange, while slists and 
dlists are, by definition, ForwardRanges and BidirectionalRanges 
respectively. So I'm afraid there's no standard function to order 
them out-of-the-box.
That's a limitation of many languages (e.g. C++ sort requires a 
RandomIterator, which  forward_list and list do not provide).

What's the best workaround depends on your exact use case.


More information about the Digitalmars-d-learn mailing list