Jump to content
  • Advertisement
Sign in to follow this  
ph33r

How do I sort by two data types.within a structure.

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

I've got a particle system that has, isAlive and texture for variables. I need to sort it first by isAlive then sort it by texture. But still maintaining isAlive order, basically all the alive particles should be at the beginning, and inorder of there texture(so i can batch by texture). Anyone know how I could do this? Perhaps a different data structure support this already.

Share this post


Link to post
Share on other sites
Advertisement
You could define the less than operator as:

if(lhs.isAlive == rhs.isAlive)
return lhs.tex < rhs.tex;
return lhs.isAlive > rhs.isAlive



Then simply sort it using that operator (or use the STL to sort it using that operator)

Dwiel

Edit: sorted isAlive backwards

[Edited by - Tazzel3D on November 28, 2004 7:47:34 PM]

Share this post


Link to post
Share on other sites
The simplest solution is to write your sorting criterion to do the right thing:


#include <algorithm>

struct CompareParticles
: public std::binary_function<Particle, Particle, bool>
{
inline operator()(const Particle& lhs, const Particle& rhs) const
{
return (lhs.isAlive > rhs.isAlive) ||
(lhs.isAlive == rhs.isAlive && lhs.texture_id < rhs.texture_id);
}
};

std::vector<Particle> particles;
std::sort(particles.begin(), particles.end(), CompareParticles());


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!