Sorting vectors

Started by
8 comments, last by DividedByZero 11 years, 8 months ago
Hi guys,

I have a vector of classes. One of the variables in the class instance is nZorder (a number between 1 & 100).

I want to be able to do a, one off, sort the vectors.

I have found reference to the sort() function. But, am not sure how to use it.

I have this, but it wount compile due to the last parameter.

sort(VectorAsset.begin(),VectorAsset.end(),VectorAsset.nZorder);

Any tips on how to use this function would be awesome.
Advertisement
struct MySortFunctor
{
bool operator() (const MyClass& op0, const MyClass& op1) const
{
return op0.nZorder < op1.nZorder; // or whatever
}
};

sort(VectorAsset.begin(),VectorAsset.end(), MySortFunctor());

should do the trick, minus possible typos. Could be much easier if your compiler supports lambdas.
Thanks for the help. No idea what it does though :)

I get a couple of errors when compiling, any chance of a hand on that too?

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '&'
error C2065: 'op0' : undeclared identifier
error C2228: left of '.nZorder' must have class/struct/union
error C2065: 'op1' : undeclared identifier
error C2228: left of '.nZorder' must have class/struct/union
If you copied the above code verbatim you have to replace MyClass with whatever is inside the vectors. If you have already done that and the errors persists the struct must be declared where whatever MyClass actually is is completely known.
Cool, thanks for the advice. Everything compiles great now!

Although it doesn't seem to have resorted anything.

Does sort() permenantly reshuffle things into the new order or is it just for that call (or something)?
Use your debugger to inspect the vector right after the sort call. Unless the nZorder are not in the state you expect them to be or you added a faulty sort functor, the vector should be sorted. Check that.
If the vector is sorted right after the call but whatever happens later does not see the correctly sorted vector, all kinds of things could be wrong. You could be, for example, sort a copy of the vector in a function call, discard the sorted version at the end of the function and use the unsorted original vector afterwards. But that is impossible to say without code.
reference: http://www.engr.sjsu...tt/SortFind.htm just go down until it talks about sort


this is how i learned to use sort. using a function that is basically a < operator

bool lessOp (YourClass a, YourClass b)
{
if (a.attribute < b.attribute) return true
else return false;
}

then with your vector of objects, just call the sort function from the algorithm library

sort (vec.begin(), vec.end(), lessOp); /////// you're passing a pointer to your lessOp function; using this pointer sort will call your function to compare the objects

or doing what the website i used above, just create a operator< function for you classes like this:

bool operator<(const Cperson& left, const Cperson& right)
{
return left.lastname < right.lastname;
}

now whenever you have 2 objects of that class and you do this var1<var2 it will called that function above.

just use your class instead of Cperson
Always improve, never quit.

Use your debugger to inspect the vector right after the sort call. Unless the nZorder are not in the state you expect them to be or you added a faulty sort functor, the vector should be sorted. Check that.
If the vector is sorted right after the call but whatever happens later does not see the correctly sorted vector, all kinds of things could be wrong. You could be, for example, sort a copy of the vector in a function call, discard the sorted version at the end of the function and use the unsorted original vector afterwards. But that is impossible to say without code.


Cool, thanks for the advice. I'll have a look at that.
It seems to be more a case of nZorder not being in the state I expected. The values are, indeed, not what they shoud be.

I'll have a look and let you know how I go.

Thanks for persisting with me :)
LOL - it helps when you dont have cAsset.nZorder=nZorder; commented out when creating the instance. :)

BitMaster - You have been an awesome help! All is working perfectly now!

I can't thank you enough! +1 to all of your posts!

This topic is closed to new replies.

Advertisement