Sorting a zipped range

Ben Jones fake at fake.fake
Thu Feb 27 13:09:44 PST 2014


One of the cool features of D that I've attempted before in c++ 
is to sort a set of ranges in lockstep like (not syntactically 
correct):

auto a = [5,4,3,2,1]
auto b = [1,2,3,4,5]


auto sorter = (Tuple x, Tuple y) => x.get(0) < y.get(0);
sort!sorter(zip(a,b));

-> a = [1,2,3,4,5], b = [5,4,3,2,1]

In c++ this seems basically impossible to do correctly with 
std::sort because it seems impossible to make a random access 
iterator type that can be returned from zip() that has the 
correct semantics (see here: 
http://stackoverflow.com/questions/13840998/sorting-zipped-locked-containers-in-c-using-boost-or-the-stl)

My question is:  which features of the D range abstraction allow 
zip to work the way we expect?  What C++isms were left behind to 
make this work?



More information about the Digitalmars-d-learn mailing list