www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - does array.sort work at all for opCmp?

reply z gg.com writes:
dmd 129:

sortbug.exe
1.000000 3.000000 2.000000
2.000000 3.000000 1.000000


sortbug.d
------------------------------------------
class A {
public:
float value;

this(float v) {value=v;}

int opCmp(A o) {
if (this.value < o.value)  return -1;
if (this.value > o.value)  return  1;
return 0;
}

}
int main(char[][] args)
{
int i;
A[3] arr;

arr[0] = new A(1.0);
arr[1] = new A(3.0);
arr[2] = new A(2.0);
for (i=0; i<3; i++) { printf("%f ", arr[i].value); }
printf("\n");
arr.sort;
for (i=0; i<3; i++) { printf("%f ", arr[i].value); }
printf("\n");

return 0;
}
------------------------------------------
Aug 10 2005
parent reply zwang <nehzgnaw gmail.com> writes:
You need to define int opCmp(Object o){ ... }

z gg.com wrote:
 dmd 129:
 
 sortbug.exe
 1.000000 3.000000 2.000000
 2.000000 3.000000 1.000000
 
 
 sortbug.d
 ------------------------------------------
 class A {
 public:
 float value;
 
 this(float v) {value=v;}
 
 int opCmp(A o) {
 if (this.value < o.value)  return -1;
 if (this.value > o.value)  return  1;
 return 0;
 }
 
 }
 int main(char[][] args)
 {
 int i;
 A[3] arr;
 
 arr[0] = new A(1.0);
 arr[1] = new A(3.0);
 arr[2] = new A(2.0);
 for (i=0; i<3; i++) { printf("%f ", arr[i].value); }
 printf("\n");
 arr.sort;
 for (i=0; i<3; i++) { printf("%f ", arr[i].value); }
 printf("\n");
 
 return 0;
 }
 ------------------------------------------
 
 
Aug 10 2005
parent reply Niko Korhonen <niktheblak hotmail.com> writes:
zwang wrote:
 You need to define int opCmp(Object o){ ... }
...and I think it should be a compiler warning trying to define only opCmp(MyObj) without defining opCmp(Object) for objects. It would save us from so much hassle. The same thing for not defining toHash when defininng opEquals and so forth. -- Niko Korhonen SW Developer
Aug 11 2005
parent z gg.com writes:
zwang wrote:
 You need to define int opCmp(Object o){ ... }
Does D support covariant argument type for class method?
Aug 11 2005