Jump to page: 1 2
Thread overview
Using array.sort
Jan 24, 2007
Heinz
Jan 24, 2007
torhu
Jan 24, 2007
Heinz
Jan 24, 2007
Carlos Santander
Jan 25, 2007
Heinz
Jan 25, 2007
Heinz
Jan 25, 2007
Heinz
Jan 25, 2007
Derek Parnell
Jan 25, 2007
Frits van Bommel
Jan 26, 2007
Frits van Bommel
January 24, 2007
What does this sort property do? how can i use it? how do i implement opCmp. thanks in advance
January 24, 2007
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.
January 24, 2007
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?
January 24, 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
January 25, 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.
January 25, 2007
Heinz wrote:
> 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.

opCmp needs only return the sort-order of an object in relation to another given object. Using a simpler example:

class Number {
  int i;

  int opCmp (Object obj) {
    if (auto other = cast(Number) obj) {
      return this.i - other.i;
    }
    else {
      throw new Exception("Can only compare Number with another Number.");
    }
  }
}

Otherwise the .sort property would have no idea how to order the objects (there really is no generic means to do this).  IIRC, there is/was a default Object.opCmp which compared the address of objects, but this is really useless in terms of proper sorting.  (Although it does have the usefulness of making all classes available as associative array keys.)

-- Chris Nicholson-Sauls
January 25, 2007
Chris Nicholson-Sauls Wrote:

> Heinz wrote:
> > 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.
> 
> opCmp needs only return the sort-order of an object in relation to another given object. Using a simpler example:
> 
> class Number {
>    int i;
> 
>    int opCmp (Object obj) {
>      if (auto other = cast(Number) obj) {
>        return this.i - other.i;
>      }
>      else {
>        throw new Exception("Can only compare Number with another Number.");
>      }
>    }
> }
> 
> Otherwise the .sort property would have no idea how to order the objects (there really is no generic means to do this).  IIRC, there is/was a default Object.opCmp which compared the address of objects, but this is really useless in terms of proper sorting.  (Although it does have the usefulness of making all classes available as associative array keys.)
> 
> -- Chris Nicholson-Sauls


Hi Chris, i still don't get it hahaha. Replace the int in your example by a char[] str; How the hell opCmp can compare a string, what value of type int should return? thx
January 25, 2007
Heinz wrote:
> Chris Nicholson-Sauls Wrote:
> 
>> Heinz wrote:
>>> 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.
>> opCmp needs only return the sort-order of an object in relation to another given object. Using a simpler example:
>>
>> class Number {
>>    int i;
>>
>>    int opCmp (Object obj) {
>>      if (auto other = cast(Number) obj) {
>>        return this.i - other.i;
>>      }
>>      else {
>>        throw new Exception("Can only compare Number with another Number.");
>>      }
>>    }
>> }
>>
>> Otherwise the .sort property would have no idea how to order the objects (there really is no generic means to do this).  IIRC, there is/was a default Object.opCmp which compared the address of objects, but this is really useless in terms of proper sorting.  (Although it does have the usefulness of making all classes available as associative array keys.)
>>
>> -- Chris Nicholson-Sauls
> 
> 
> Hi Chris, i still don't get it hahaha. Replace the int in your example by a char[] str; How the hell opCmp can compare a string, what value of type int should return? thx

Return any value less than 0 for "less than", 0 for "equal to", and any value greater than zero for "greater than".  (Most people default to -1,0,1.)  For a char[] you'd have to decide how you want to order characters... but one generic means is just to compare their character codes.  So something like:

class myclass {
  char[] cs;

  int opCmp (Object obj) {
    char oc ;

    if (auto other = cast(myclass) obj) {
      foreach (i, c; cs) {
        oc = other.cs[i];
        if (c < oc) {
          return -1;
        }
        else if (c > oc) {
          return 1;
        }
      }
      return 0;
    }
    else {
      throw new Exception("Can only compare myclass with myclass.");
    }
  }
}

-- Chris Nicholson-Sauls
January 25, 2007
Chris Nicholson-Sauls Wrote:

> Heinz wrote:
> > Chris Nicholson-Sauls Wrote:
> > 
> >> Heinz wrote:
> >>> 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.
> >> opCmp needs only return the sort-order of an object in relation to another given object. Using a simpler example:
> >>
> >> class Number {
> >>    int i;
> >>
> >>    int opCmp (Object obj) {
> >>      if (auto other = cast(Number) obj) {
> >>        return this.i - other.i;
> >>      }
> >>      else {
> >>        throw new Exception("Can only compare Number with another Number.");
> >>      }
> >>    }
> >> }
> >>
> >> Otherwise the .sort property would have no idea how to order the objects (there really is no generic means to do this).  IIRC, there is/was a default Object.opCmp which compared the address of objects, but this is really useless in terms of proper sorting.  (Although it does have the usefulness of making all classes available as associative array keys.)
> >>
> >> -- Chris Nicholson-Sauls
> > 
> > 
> > Hi Chris, i still don't get it hahaha. Replace the int in your example by a char[] str; How the hell opCmp can compare a string, what value of type int should return? thx
> 
> Return any value less than 0 for "less than", 0 for "equal to", and any value greater than zero for "greater than".  (Most people default to -1,0,1.)  For a char[] you'd have to decide how you want to order characters... but one generic means is just to compare their character codes.  So something like:
> 
> class myclass {
>    char[] cs;
> 
>    int opCmp (Object obj) {
>      char oc ;
> 
>      if (auto other = cast(myclass) obj) {
>        foreach (i, c; cs) {
>          oc = other.cs[i];
>          if (c < oc) {
>            return -1;
>          }
>          else if (c > oc) {
>            return 1;
>          }
>        }
>        return 0;
>      }
>      else {
>        throw new Exception("Can only compare myclass with myclass.");
>      }
>    }
> }
> 
> -- Chris Nicholson-Sauls


Thanks chris, i finally understood. This example is much clearer than the other one. We could also use in this case std.string.icmp(), as it returns the same values.

Thanks man, good luck.
January 25, 2007
On Wed, 24 Jan 2007 22:55:57 -0500, Heinz wrote:


> Hi Chris, i still don't get it hahaha. Replace the int in your example by a char[] str; How the hell opCmp can compare a string, what value of type int should return? thx

This is a good question. It all depends on how you want to define when one string is less than another.

Here is one definition, just for example purposes.

import std.stdio;

 class myclass {
    char[] cs;
    this(char[] d) { cs = d.dup; }

    int opCmp (Object obj)
    {
       // Strings contain unicode code-points.
       // Shorter strings compare "less than" longer strings.
       // Equal length strings compare respective unicode code-points.

       if (cs.length < (cast(myclass)obj).cs.length)
           return -1;
       if (cs.length > (cast(myclass)obj).cs.length)
           return  1;
       if (cs == (cast(myclass)obj).cs)
           return 0;

       // Ok, so they are different. Now we look at each code-point.
       dchar[] temp_self  = std.utf.toUTF32(cs);
       dchar[] temp_other = std.utf.toUTF32((cast(myclass)obj).cs);

       foreach(int i, dchar c; temp_self)
       {
          dchar oc;
          oc = temp_other[i];
          if (c < oc)
             return -1;
          if (c > oc)
             return  1;
       }
       throw new Error("Failed to find difference. Should never happen.");
    }
 }

void show(myclass[] Data, char[] Title)
{
   writefln("\n\n%s\n--------------", Title);
   foreach(myclass x; Data)
   {
        writefln("%s", x.cs);
   }
}

void main()
{
   myclass[] A;
   A ~= new myclass("one");
   A ~= new myclass("two");
   A ~= new myclass("three");
   A ~= new myclass("four");
   A ~= new myclass("five");
   A ~= new myclass("six");
   A ~= new myclass("seven");
   A ~= new myclass("eight");
   A ~= new myclass("nine");
   A ~= new myclass("ten");

   show(A, "Before");
   A.sort;
   show(A, "After");

}

 =============

C:\temp>dmd -run test.d


Before
--------------
one
two
three
four
five
six
seven
eight
nine
ten


After
--------------
one
six
ten
two
five
four
nine
eight
seven
three

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
25/01/2007 4:03:16 PM
« First   ‹ Prev
1 2