Using array.sort

Carlos Santander csantander619 at gmail.com
Wed Jan 24 14:37:28 PST 2007


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


More information about the Digitalmars-d-learn mailing list