Help Needed Sorting A Struct


Guyfawkes

Certified Idiot :)
I have gone braindead trying to sort some data in a struct for the highscores in my game. Here is the struct I am using:

Code:
struct highscores	
{
	char PlayerName[18];
	double winnings;
	double highestoffer;
	double boxvalue;
};
struct highscores HighscoresUS[11];
struct highscores HighscoresUK[11];

I want to sort the highscores by the amount won (called 'winnings' in the struct) but I cant seem to get it working. I would have pasted the code I am currently using but in a fit of anger I deleted it :)

Does anyone have a routine that can help me out?
 
Guyfawkes posted on Jul 28 2006 at 07:22 AM said:
I have gone braindead trying to sort some data in a struct for the highscores in my game. Here is the struct I am using:

Code:
struct highscores	
{
	char PlayerName[18];
	double winnings;
	double highestoffer;
	double boxvalue;
};
struct highscores HighscoresUS[11];
struct highscores HighscoresUK[11];

I want to sort the highscores by the amount won (called 'winnings' in the struct) but I cant seem to get it working. I would have pasted the code I am currently using but in a fit of anger I deleted it :)

Does anyone have a routine that can help me out?
Write a function that compares two highscores structures and then call qsort.
 
Last edited by a moderator:
i was trying the qsort method but something kept buggering up so I got bored trying to find the problem as i have been trying to fix this off and on for the past few days.

problem is there are prizes of 1 cents, 50 cents etc. i could cheat and make the default highscore of $1 so i can convert to int and not worry about cents as you have to win more than $1 to get the highscore.
 
Guyfawkes posted on Jul 28 2006 at 08:09 AM said:
i was trying the qsort method but something kept buggering up so I got bored trying to find the problem as i have been trying to fix this off and on for the past few days.
I'm not sure I have the cure for boredom, but qsort is pretty easy to use...

Code:
int CompareScores(const void *p1, const void *p2)
{
  struct highscores *sc1, *sc2;
  sc1 = (struct highscores *) p1;
  sc2 = (struct highscores *) p2;
  if(sc1->winnings == sc2->winnings)
	return 0;
  if(sc1->winnings > sc2->winnings)
	return 1;
  return -1;
}

.....

qsort(HighscoresUK, NumberOfValidEntriesInHighscoresUKArray, sizeof(struct highscores), CompareScores);
 
Last edited by a moderator:
I'm using something simple, bad written to sort the things. I've got a struct 'card' with a property called 'wert' that has to be sorted. I just check about the value is bigger than the first one, if it is, then set the new one the first one. Else check about it is bigger than the second one, if it is, move all others one to the back, then set this the first one. If it isn't, check about it is bigger than the second one, if it is, move the second one and all worse one back, and set the new the second one etc.

Rather ugly, but it works flawless. And I haven't known qsort until now..
 
Guyfawkes posted on Jul 28 2006 at 08:09 AM said:
i was trying the qsort method but something kept buggering up so I got bored trying to find the problem as i have been trying to fix this off and on for the past few days.

problem is there are prizes of 1 cents, 50 cents etc. i could cheat and make the default highscore of $1 so i can convert to int and not worry about cents as you have to win more than $1 to get the highscore.

Could use the least two digits as the cents, ie 100 = $1, 1 = 1 cent, etc
Of course it's possible this is more trouble than it's worth, so meh.
 
Last edited by a moderator:
Sorry, i accientally used p1->winnings in the original code, then changed it to sc1->winnings which is correct. From the error message you're getting i think you're using the p1->winnings code.

Edit: whoops! The post I was replying to with this disappeared! Did I dream it?
 
Guyfawkes posted on Jul 28 2006 at 03:09 PM said:
i was trying the qsort method but something kept buggering up so I got bored trying to find the problem as i have been trying to fix this off and on for the past few days.

problem is there are prizes of 1 cents, 50 cents etc. i could cheat and make the default highscore of $1 so i can convert to int and not worry about cents as you have to win more than $1 to get the highscore.

Multiply all values by 100.

Then just divide by 100 for display purposes.
 
Last edited by a moderator:
yeah i posted the problem then noticed you had updated the code so i deleted my post. just gonna have something to eat then I will try it out.
 
Back
Top