Static arrays and std.algorithm.sort

Stanislav Blinov stanislav.blinov at gmail.com
Thu Feb 20 10:32:56 PST 2014


Note that you can create your own overload. Though it has to be 
done "the long way", because dmd doesn't (yet?) allow overloading 
templates imported from other modules:

import std.stdio;
import std.algorithm;
import std.traits;

template sort(alias less = "a < b", SwapStrategy ss = 
SwapStrategy.unstable)
{
     auto sort(Arr)(ref Arr arr) if (isStaticArray!Arr) {
         return std.algorithm.sort!(less, ss)(arr[]);
     }

     auto sort(Range)(Range r) if (!isStaticArray!Range) {
         return std.algorithm.sort!(less, ss)(r);
     }
}

void main ()
{
     int[5] a = [ 9, 5, 1, 7, 3 ];
     int[]  b = [ 4, 2, 1, 6, 3 ];

     sort(a);
     sort(b);
     writeln(a);
     writeln(b);
}


More information about the Digitalmars-d-learn mailing list