On Saturday, 29 June 2013 at 14:20:05 UTC, Ali Çehreli wrote:
Not knowing whether it applies to your case, the following is
one almost correct way of writing opCmp:
int opCmp(ref const Vector3D vec) {
return cast(int)(x != vec.x
? x - vec.x
: (y != vec.y
? y - vec.y
: z - vec.z));
}
Ali
This is the closes thing to what the OP wrote, but it is a *very*
(IMO) strange way to sort 3D elements.
In basic English, it's:
Whoever has the smallest X, or, if tied,
Whoever has the smallest Y, or, if tied,
Whoever has the smallest Z
OP: Is this how you wanted to sort?
This is strange to me because ordering is (usually) tied to a
norm, where basically, opCmp returns whoever has the smallest
norm. Yet this ordering isn't really tied to any norm.
OP: Can you confirm how you want to sort? Usually, 3DVector are
sorted using any of Euclidian, Manhatan, Maximum or Minimum norm:
https://en.wikipedia.org/wiki/Norm_(mathematics)#Examples
If you can define a "norm" function, then opCmp becomes:
--------
int opCmp(in Vector3D iRhs)
{
auto nLhs = this.norm();
auto nRhs = iRhs.norm();
return (nLhs > nRhs) - (nLhs < nRhs);
}
--------
This is standard "boilerplate" opCmp. It avoids branching too.