Using array.sort
Heinz
billgates at microsoft.com
Wed Jan 24 19:11:46 PST 2007
Carlos Santander Wrote:
> Heinz escribió:
> > torhu Wrote:
> >
> >> Heinz wrote:
> >>> What does this sort property do? how can i use it? how do i implement opCmp. thanks in advance
> >> .sort sorts an array, using some default sort order. If you want to
> >> change the order, you implement opCmp.
> >>
> >>
> >> opCmp has got these signatures, think. It doesn't seem to be documented
> >> much:
> >>
> >> class C {
> >> // the argument is of type Object, not C
> >> int opCmp(Object other);
> >> }
> >>
> >> or:
> >>
> >> struct S {
> >> int opCmp(S other);
> >> }
> >>
> >>
> >> opCmp() has to return less than zero if it's own object is smaller, more
> >> than zero if 'other' is smaller, and zero if they are equal.
> >>
> >> You can't define opCmp for any other types, if you want to change the
> >> sort order of ints, you have to write a separate sort function.
> >
> > So, lets assume we have the following class:
> >
> > class myclass
> > {
> > char[] cs;
> > }
> >
> > and then we have a dinamic array of myclass, can we sort this array by the cs property?
>
> Yes, you would have to write an opCmp that does that.
>
> --
> Carlos Santander Bernal
Hi,
Do i have to declare opCmp(Object) and it'll do it automatically? or
Do i have to write the algorithm to sort the classes by the cs property? if so, what's the sense of having a sort property if i have to implement a propietary function, it acts as a link.
More information about the Digitalmars-d-learn
mailing list