How to override opCmp when two different sorts are needed?
Jarrett Billingsley
kb3ctd2 at yahoo.com
Tue Aug 14 13:33:54 PDT 2007
"John Bartelt" <bartelt at cox.net> wrote in message
news:f9quel$rou$1 at digitalmars.com...
>I have a class that contains two variables. Sometimes I need to sort an
>array of the class based on variable 1, and later in the same progam sort
>the array based on variable 2.
>
> Can someone give me an idea of how to do this (smartly, efficiently,
> cleverly, polymorphically, ...)?
>
> Best idea that I've had so far is to use a static flag in the class to
> indicate which sort is desired, and then to have the overloaded opCmp()
> check the flag before comparing.
Write your own sort routine which takes a given predicate. :\
Tango provides such a routine in tango.core.Array; phobos provides no such
analogue. With phobos, either write your own routine, or use
std.c.stdlib.qsort. I'll let you decide which is more elegant.
More information about the Digitalmars-d
mailing list