Sorting an array

Oskar Linde oskar.lindeREM at OVEgmail.com
Tue Feb 6 00:32:31 PST 2007


Michiel wrote:
> When I use array.sort, which sorting algorithm does D use? Can the programmer
> overwrite the implementation of this function?

Just write a library replacement[1]. The only difference is that it will 
have to be called as:

array.sort() instead of array.sort

The advantages are several. It will be faster than the built in sort and 
you will have the possibility of making it support custom ordering 
predicates and more. IMHO, the built in .sort and .reverse properties(?) 
should be removed as identical in syntax and infinitely more flexible 
library implementations are possible.

For a sample implementation, see my old and dusty std.array proposal:
http://www.csc.kth.se/~ol/array.d
Ugly DDoc:
http://www.csc.kth.se/~ol/array.html

it includes:
array.sort()
array.sort(predicate wrongOrder)
array.stableSort()
array.stableSort(predicate wrongOrder)

And in-place versions:
array.doSort()
array.doSort(predicate wrongOrder)
array.doStableSort()
array.doStableSort(predicate wrongOrder)

I believe it still compiles, but no guarantees as it was written for DMD 
0.149 or thereabouts.

/Oskar



More information about the Digitalmars-d mailing list