Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

[C++] Vector sorting

This topic is 3040 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

vector<Quad3D*> m_quadArray;
QuadSorter m_quadSorter;


I have an array of Quad3D objects stored in a vector. Now I need to sort this array of objects..

1. First by Quad3D->Texture->Id()
2. Second by Quad3D->Id()
3. Third by Quad3D->GetPosition().Z
4. If all of the above are equal I need them to stay in the order they were added

My sort class is currently

class QuadSorter
{
public:
bool operator() (Quad3D* quad1, Quad3D* quad2) { return (quad1->Texture()->Id() < quad2->Texture()->Id()); }
};


And to sort I call

sort(m_quadArray.begin(), m_quadArray.end(), m_quadSorter);


How can I modify the QuadSorter class to meet the criteria above?

Share this post


Link to post
Share on other sites
Advertisement
Try:
bool operator()( const Quad3D* quad1, const Quad3D* quad2 ) const {
if ( quad1->Texture()->Id() != quad2->Texture()->Id() ) return ( quad1->Texture()->Id() < quad2->Texture()->Id() );
if ( quad1->Id() != quad2->Id() ) return ( quad1->Id() < quad2->Id() );
...
return false;
}

Quote:
4. If all of the above are equal I need them to stay in the order they were added

For this you'll need to use std::stable_sort instead of std::sort.

Why all these consts?
Hope this helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!