std.array suggestion

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Mar 9 08:27:46 PST 2006


Stewart Gordon wrote:
> Oskar Linde wrote:
> <snip>
>> T[] doSort(T[], delegate|function wrongOrder(T,T));
> <snip>
> 
> You've left off the return type of wrongOrder.  Moreover, why the name 
> wrongOrder?  To simply return whether or not these two elements are out 
> of order?  Why not use what people are used to, i.e. a function that 
> returns -ve, 0 or +ve for <, = or > respectively?

The return value would be anything usable as a condition in a 
conditional function if(wrongOrder(a,b)) {...}

The reason for not having -ve,0,+ve is that you only need a binary 
predicate for sorting. It is also in many cases simpler to define a 
ordering predicate than a 3-valued ordering. The name wrongOrder just 
helps as a reminder of what the predicate should return.

names.sort(delegate bool(char[] a, char[] b) { return a > b; });

would sort the names in alphabetical order. To sort in reverse 
alphabetical order:

names.sort(delegate bool(char[] a, char[] b) { return a < b; });

records.sort(delegate bool(Person a, Person b) { return a.name < b.name;});

/Oskar



More information about the Digitalmars-d mailing list