Sorting an Array

torhu fake at address.dude
Tue Jul 10 11:57:11 PDT 2007

okibi wrote:
> This does seem like the best way, but I'm not sure how I would go about implement Quicksort into that code. Could you please give me some pointers?

I often use C's quicksort, found in std.c.stdlib.

void sort(T)(T[] array, bool function(T a, T b) compare) {

     // wrap compare in a C function
     static extern(C) int cmp(void* a, void* b) {
         return compare(*cast(T*)a, *cast(T*)b);

     qsort(array.ptr, array.length, array[0].sizeof, &cmp);

compare has to return a negative number if a is smaller, positive is b 
is smaller, or zero if they are equal.

More information about the Digitalmars-d-learn mailing list