Sign in to follow this  

Sorting vectors

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

If you intended to correct an error in the post then please contact us.

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.

[code]sort(VectorAsset.begin(),VectorAsset.end(),VectorAsset.nZorder);[/code]

Any tips on how to use this function would be awesome.

Share this post


Link to post
Share on other sites
[code]struct MySortFunctor
{
bool operator() (const MyClass& op0, const MyClass& op1) const
{
return op0.nZorder < op1.nZorder; // or whatever
}
};

sort(VectorAsset.begin(),VectorAsset.end(), MySortFunctor());[/code]
should do the trick, minus possible typos. Could be much easier if your compiler supports lambdas.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
reference: [url="http://www.engr.sjsu.edu/wbarrett/SortFind.htm"]http://www.engr.sjsu...tt/SortFind.htm[/url] 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 Edited by arkane7

Share this post


Link to post
Share on other sites
[quote name='BitMaster' timestamp='1343737193' post='4964830']
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.
[/quote]

Cool, thanks for the advice. I'll have a look at that.

Share this post


Link to post
Share on other sites
LOL - it helps when you dont have [b]cAsset.nZorder=nZorder; [/b]commented out when creating the instance. :)

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

I can't thank you enough! [b]+1[/b] to all of your posts!

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this