Public Group

Sorting vectors

This topic is 2178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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.

Share on other sites
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

Share on other sites
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.

Share on other sites
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)?

Share on other sites
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.

Share on other sites
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.

Share on other sites

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.

Share on other sites
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

Share on other sites
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!

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631751
• Total Posts
3002086
×