How to define and use a custom comparison function

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 16 02:24:21 PDT 2014


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.)


More information about the Digitalmars-d-learn mailing list