How to sort a range
rcorre via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Mar 8 19:05:52 PST 2016
I was in a situation where I wanted to remove duplicates from an
OnlyResult.
To do this with uniq, I needed to sort it. OnlyResult doesn't
satisfy the template constraints of sort, but this seems easy
enough to fix. I made front, back, and opIndex return by ref.
With this, the following compiles:
assert(only(3,1,2).sort.equal(only(1,2,3)));
However, it fails with:
core.exception.AssertError at std/algorithm/sorting.d(1052):
Failed to sort range of type OnlyResult!(int, 3LU)
So, if you have a range for which sort compiles, what does it
take to make sorting actually work?
For reference, my two attempts were:
https://github.com/rcorre/phobos/commit/d89b3cfab7a0938e178a506b4ceb8faae6ecbfe2
https://github.com/rcorre/phobos/commit/512d9b8db6f311db6a9b6ccb077a691cec66ce70
More information about the Digitalmars-d-learn
mailing list