Sign in to follow this  

Sorting array of structs by string

Recommended Posts

I have a struct defined as

[code]struct GameInfo
GameType Type;
const char *Name;
int StartScoreMode;
int EndScoreMode;
const char *ScoreModeName[4];

Using this I have

[code]GameInfo g_gameInfo[] =
// Type Name Start Score Mode End Score Mode Score Mode Name
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now I have a need to access a sorted list of these by Name. So I created a Sort function

[code]struct SortGame
bool operator()(GameInfo* a, GameInfo* b) { (strcmp(a->Name, b->Name) < 0); }

Now I create a vector of pointers to have a sorted list of games

[code]for(int i=0; i<GAMETYPE_COUNT; i++)

std::sort(g_gameInfoArray.begin(), g_gameInfoArray.end(), SortGame());

for(int i=0; i<GAMETYPE_COUNT; i++)

The problem is it's not sorting the games right. When I print out the name's in g_gameNameArray it lists them as

Rank & File
Number 10

What is going wrong here?

Share this post

Link to post
Share on other sites
your sort function should return a Boolean value. Currently, it doesn't return anything. You should be receiving compiler errors on this, I don't even think it should compile as is.

Should be
struct SortGame
bool operator()(GameInfo* a, GameInfo* b) { return strcmp(a->Name, b->Name) < 0; }

Share this post

Link to post
Share on other sites

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