Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Prairie

stl list sort member function?

This topic is 6017 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 did a search and it seems that the other questions asked revolved around over-riding the member function. Let''s say I have a list of a certain class. How do I sort the list based on a variable in each class. I can''t find any info, and am really new to the stl. Thanks; Prairie

Share this post


Link to post
Share on other sites
Advertisement
The answer is that you either override the > < operators for the class, or you provide a function to the sort function. Here''s what I do for a vector of dialog windows to sort based on a Z value:

dxtarget.h
  
class CDxTargetVector : public CDxObj
{
static bool zgreater(const CDxTarget* pFirst, const CDxTarget* pSecond){
return (pFirst->GetZ() > pSecond->GetZ());
}
//...

};


dxtarget.cpp
  
void CDxTargetVector::Sort()
{
std::sort(m_vctData.begin(), m_vctData.end(), zgreater);
}

Share this post


Link to post
Share on other sites
The title refers to std::list. There''s a specific sort function for list because you can''t use std::sort on list, you have to use std::list::sort. But it''s the same answer: either override the operator < for your contained data (if possible), or provide your own predicate into the std::list::sort function.

Share this post


Link to post
Share on other sites

  • 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!