Sorting by multiple attributes problem

Hi fellow code warriors,

I have the following comparison functor :

1
2
3
4
5
6
7
8
	struct sizeSortDescFunctor{
		bool operator ()(const Sphere & i, const Sphere & j){
			if(i.getSize() && j.getSize()){
				return (i.getMiddlePoint().getZ() > j.getMiddlePoint().getZ());
			}else
				return i.getSize();
		}
	};


The idea is to sort "spheres" according to their radius and the a dimension if the radius are the same.

The radius is a bool while the dimension is a double.

The above functor works for the big spheres i.e. where the radius is true.

All the small spheres remain under the big ones unsorted. I would expect that in order to extend this I would simply have to add this :

1
2
3
4
5
6
7
8
9
	//Functor to sort spheres by radius size
	struct sizeSortDescFunctor{
		bool operator ()(const Sphere & i, const Sphere & j){
			if((i.getSize() && j.getSize()) || (!i.getSize() && !j.getSize())){
				return (i.getMiddlePoint().getZ() > j.getMiddlePoint().getZ());
			}else
				return i.getSize();
		}
	};


But this doesn't work. It produces mixed results.

I am sure it's possible I just can't find my error.
 
if(i.getSize() && j.getSize()){


The above line of code is equivalent to if(i.getSize()!=0 && j.getSize()!=0){ but then later you say this line "The idea is to sort "spheres" according to their radius and the a dimension if the radius are the same."

Above line of code is not doing checking for "radius are the same" logic.

Thanks for the reply sohguahn,

I am not sure I follow though. Since the getSize returns a bool how would someone check whether these two values are the same?

Is this wrong ? :

if( (boolOne && boolTwo) || (!boolOne && !boolTwo)
Update :

After many hours of fiddling with this problem I decided to go to the most basic things :

Print out before sorting :


46,7 40,1 -502,36 0
53,74 31,73 -503,42 0
59,53 22,35 -504,49 0
63,65 12,16 -505,52 0

Last column determines size of sphere and is a boolean

Then the following "magic" function is used :

std::sort(this->spheres.begin(), this->spheres.end(), SphereSearch::sizeSortDescFunctor());

Where the magic functor is this :

1
2
3
4
5
struct sizeSortDescFunctor{
	bool operator ()(const Sphere i, const Sphere j){
		return (i.getMiddlePoint().getZ() > j.getMiddlePoint().getZ());
	}
};


Print out after magic sorting :

46,7 40,1 -502,36 204
53,74 31,73 -503,42 204
59,53 22,35 -504,49 204
63,65 12,16 -505,52 204

So the boolean somehow magically instead of 0 now prints 204?!!!?!?!?!?!

I seriously have no idea what the hell is happening lol!
What do your copy constructor and assignment operator look like for Sphere, and why on earth is radius a boolean????
Sorry I didn't know getSize() is implemented as a boolean. The name indicate it is some long or int or float isn't it ? Just like jsmith I am wondering why is radius a boolean? I miss out something?
What do your copy constructor and assignment operator look like for Sphere, and why on earth is radius a boolean????


That was it. The copy constructor. Sorry not my code. Don't know why it is implemented like this. In the copy constructor everything BUT the boolean was copied.. When I changed this everything worked...

Sorry I didn't know getSize() is implemented as a boolean. The name indicate it is some long or int or float isn't it ? Just like jsmith I am wondering why is radius a boolean? I miss out something?


No you didn't. Bad code is bad.

Thanks for the help guys. Appreciated.

Bottomline : Always check your copy contructors and assignment operators.

(If the Sphere object just does a simple member-wise copy, then you don't even need to define a copy constructor or assignment operator.)
Topic archived. No new replies allowed.