Custom sorting order

I’m porting a large library from C++ to Xojo.
Several of the objects have custom sorting.
I tried implementing the operator_compare in Xojo, but I get different results than the C++ code.
I’m suspecting that it’s to do with comparing functionally equivalent objects.

While the values compared sort the same, the objects that they reference are in a different order.

The C++ code is as follows.

[code]// sort into descending order
inline bool operator<(const ANT &a, const ANT &b)
{
if( a.a == b.a ){
return a.b < b.b;
}
return a.a < b.a;

};[/code]

After trying to have the operator_compare do it’s thing, I altered my code to try to match the above, and I used a bubble sort.
Discouraged: the xojo code below + bubble sort have the same results, both of which are different than the original C++

The code is implemented in a method named isLessThan on the object. It looks like thi.

If a = s.a Then Return b < s.b End If Return a < s.a

This doesn’t seem like it should be rocket science, and yet I’m either too foggy from lack of sleep, or I’m just missing something obvious.

Thoughts or suggestions?

Further info.

the object’s AB that I put in are:
*the first column is an non-unique and non-compared id.
You can see that the column 2 and 3 are the same in C++ and Xojo, but the first column is different, indicating that the actual objects referenced sequence differently.

7: 0 1
7: 2 0
6: 0 1
6: 1 0
5: 3 1
5: 2 0
4: 3 1
4: 1 0

in C++ I get back
7: 0 1
6: 0 1
4: 1 0
6: 1 0
7: 2 0
5: 2 0
4: 3 1
5: 3 1

in Xojo I get back
7: 0 1
6: 0 1
6: 1 0
4: 1 0
7: 2 0
5: 2 0
5: 3 1
4: 3 1

Thanks in advance!

Did you know you could use the built in sort with a delegate to do a custom sort? I ask because it doesn’t seem like that’s what you’re doing.

@Chris Halford — From your message, data is actually sorted the same in C++ and in Xojo. Having equal objects listed in a different order should not be an issue.

It is an issue though, because of the operations that follow.

Yes, Kem, I tried that first. Matching results.

@Chris Halford — OK so there is, fundamentally, a hidden key somewhere in the C++ algorithm which is not obvious from the code. You just need to find this key.

I have noticed that the C++ sorting method does not consider equality as it returns only the result of a “<” operation. This may be a hint.

Also, are you sure that you compare objects in the same order in Xojo as in C++?

hmm…

I just re-ran with the following change “<=” and I get the same result.
Funny I never find these easy solutions until after I engage the entire world, then it just clicks…

If a = s.a Then Return b <= s.b End If Return a <= s.a

“I get the same result”

Does it mean that you get the same result as before or that you get the same result in Xojo and C++?

Stphane,

I mean if I do <= then it matches the C code.
Sorry, was not more clear.