How to define and use a custom comparison function

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 16 13:49:27 PDT 2014


On Monday, 16 June 2014 at 09:24:22 UTC, Marc Schütz wrote:
> You can pass anything to the sort function that's callable, 
> including an object:
>
>     struct MyCompare {
>         SortOrder order;
>         int column;
>         bool opCall(const ref DataRow lhs, const ref DataRow 
> rhs) {
>             return order == SortOrder.ASC ?
>                 lhs[column] < rhs[column] :
>                 rhs[column] < lhs[column];
>         }
>     }
>
>     import std.algorithm;
>     MyCompare cmp(SortOrder.ASC, 10);
>     my_columns.sort!cmp;
>
> (Untested.)

That works, but it's not very idiomatic. Well, I have never seen 
a C++ style "functor" used in D template is what I'm saying. I 
don't know if that's good or bad. A more idiomatic approach would 
be to simply pass a delegate to your function.

This can either be as a pointer to member function:

struct MyCompare {
     SortOrder order;
     int column;
     bool compare(const ref DataRow lhs, const ref DataRow rhs)
{
     return order == SortOrder.ASC ?
         lhs[column] < rhs[column] :
         rhs[column] < lhs[column];
     }
}

import std.algorithm;
MyCompare cmp(SortOrder.ASC, 10);
auto dg = &cmp.compare;
my_columns.sort!dg;

Or, more generally, via a lambda, or a function with state:

import std.algorithm;
column = 10;
bool compare(const ref DataRow lhs, const ref DataRow rhs)
{
     return order == SortOrder.ASC ?
         lhs[column] < rhs[column] :
         rhs[column] < lhs[column];
     }
}
my_columns.sort!compare;


More information about the Digitalmars-d-learn mailing list