Multisort
superdan
super at dan.org
Sun Jul 6 14:59:06 PDT 2008
dsimcha Wrote:
> I've been working on implementing some statistics functions in D, and noticed
> that it would be nice if a multisort function were in Phobos. Such a function
> would take N arrays in and sort them by the first one. The common way to
> handle something like this seems to be to make a struct with the fields, make
> opCmp point to whatever field you want to sort by, and then use a regular
> sorting algorithm. This is slow and a pain. I've implemented a few different
> multisort functions, along with documentation and unit testing, and would like
> to contribute them to Phobos. I've attached the source file and the Ddoc
> documentation.
yarp i needed this 2. sounds good but it's unnecessary. sort does all that shit easily.
int[] arr1;
float[] arr2;
string[] arr3;
// sort three arrays as one
void mySwap(int* a, int* b)
{
auto i = a - arr1.ptr, j = b - arr1.ptr;
iterSwap(a, b);
swap(arr2[i], arr2[j]);
swap(arr3[i], arr3[j]);
}
sort!("a < b", SwapStrategy.unstable, mySwap)(arr1);
what happens is that whenever two doods in arr1 get swapped, the corresponding doods in arr2 and arr3 get swapped too. i don't know why the docs don't give such examples. there's just a hint "Possible uses include notifying observers, counting the number of operations, or manipulating multiple collections in lockstep." the lockstep thing gave it away.
& of course you deserve shoe heel beating for putting bubblesort in there. no matter how much you warn against its usage.
More information about the Digitalmars-d
mailing list