Jump to content
  • Advertisement

Archived

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

DirectXXX

Sorting Highscrore List with Names

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

You know highscore list has a NAME and SCORE column. I want to sort the SCORE column and their respective names should be sorted. I thought STL may help it. Or any other way?

Share this post


Link to post
Share on other sites
Advertisement
If you're using an stl list then yes you can sort it. You'll need to provide an operator, either < or >, I can't remember which right now. Then I think the list has a sort() method.

You could have something like this...


struct ScoreEntry
{
std::string Name;
int iScore;
};

bool operator < (const ScoreEntry& s1, const ScoreEntry& s2)
{
return (s1.iScore < s2.iScore);
}

std::list <ScoreEntry> ScoreList;

ScoreList.sort ();


There may be errors in that code, I'm typing from memory.

If you're using a regular array you can use qsort(), where you'll need to provide a comparison callback.

Check out MSDN for the functions so you know how they work.


Why you shouldn't use iostream.h - ever! | A Good free online C++ book


[edited by - siaspete on June 18, 2003 5:23:22 AM]

Share this post


Link to post
Share on other sites
As siaspete says you can use the sort method on the std::list if you have an appropriate operator< defined for your data type.

Or check out sgi''s stl documentation for the sort algorithm.

You''ll need to either have operator< defined, define a functor or comparison function or compose a temporary functor using the facilities provided by the <functional> header

Share this post


Link to post
Share on other sites
You understood that the std::sort algorithm is different to the sort member function on std::list?

[edited by - petewood on June 18, 2003 12:50:08 PM]

Share this post


Link to post
Share on other sites
bool operator < (const ScoreEntry& s1, const ScoreEntry& s2)
{
if(s1.iScore == s2.iScore)
return (s1.sName < s2.sName);
return (s1.iScore < s2.iScore);
}

Share this post


Link to post
Share on other sites
I'm not sure I understand why you add an additional check with == there. I don't think you can predict which score entry s1 or s2 will be when the sort function is being called, so it doesn't matter if you choose one over the other if they're equal.

Maybe I misunderstood though.

Edit: Yeah I didn't read it properly! Hmm I think I'd rather not sort the score by name if they're equal but time that the score was added to the list.


Why you shouldn't use iostream.h - ever! | A Good free online C++ book


[edited by - siaspete on June 18, 2003 7:34:42 PM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!