C
C#2mo ago
Qualy

player ranking

So,... I am meant to rank the player list based on their wins. I now made something that will sort the players after they win. Problem is it only gets executed once.
C#
void SortPlayers(string winnerName)
{
int winnerIndex = playerNames.IndexOf(winnerName);
int aboveWinnerIndex = winnerIndex - 1;
if (aboveWinnerIndex >= 0)
{
string aboveWinnerName = playerNames[aboveWinnerIndex];
if (playerWins[winnerName] > playerWins[aboveWinnerName])
{
playerNames[aboveWinnerIndex] = winnerName;
playerNames[winnerIndex] = aboveWinnerName;
}
}
UpdatePlayerList();
}
C#
void SortPlayers(string winnerName)
{
int winnerIndex = playerNames.IndexOf(winnerName);
int aboveWinnerIndex = winnerIndex - 1;
if (aboveWinnerIndex >= 0)
{
string aboveWinnerName = playerNames[aboveWinnerIndex];
if (playerWins[winnerName] > playerWins[aboveWinnerName])
{
playerNames[aboveWinnerIndex] = winnerName;
playerNames[winnerIndex] = aboveWinnerName;
}
}
UpdatePlayerList();
}
playerNames is a list playerWins is a dictionary
No description
15 Replies
tacosontitan
tacosontitan2mo ago
List of what Dictionary of what
Angius
Angius2mo ago
Do you call it more than once?
Qualy
QualyOP2mo ago
playerNames holds just the names of the players playerWins: key: players name, value: players wins
tacosontitan
tacosontitan2mo ago
(we can make intelligent assumptions, it's just better to be explicit with your types when asking for help)
Qualy
QualyOP2mo ago
I want it to run until its sorted right
Salman
Salman2mo ago
what's above played abo
Angius
Angius2mo ago
You will have to do that, then
Salman
Salman2mo ago
about
Angius
Angius2mo ago
In a loop maybe Or use LINQ Or any number of other options
Qualy
QualyOP2mo ago
when now, for example, I run it and player 4 wins, 3 and 4 just switch places instead of 4 going all the way to the top not allowed (school)
Angius
Angius2mo ago
Just sort the whole list with every match Or use some SortedList, SortedDictionary, etc If allowed Fair
Qualy
QualyOP2mo ago
does sorteddictionary sort on the value
FusedQyou
FusedQyou2mo ago
I feel like you have a better shot recreating the collection after each win than trying to fit it inside an existing list And your list doesn't need to do much. The list can just be a list of indexes that represent the index in your dictionary or whatever collection contains your players. Would you be able to extend playerNames into an object containing both the player name and their wins? That would be a much easier way to maintain the context of players since they all have a wincount.
tacosontitan
tacosontitan2mo ago
(unless it's a requirement to modify the existing list)
Qualy
QualyOP2mo ago
C#
void SortPlayers(string winnerName)
{
int winnerIndex = playerNames.IndexOf(winnerName);
int aboveWinnerIndex = winnerIndex - 1;
string aboveWinnerName = playerNames[aboveWinnerIndex];
while (aboveWinnerIndex > 0 && playerWins[winnerName] > playerWins[aboveWinnerName])
{
aboveWinnerIndex--;
aboveWinnerName = playerNames[aboveWinnerIndex];
}
playerNames[winnerIndex] = aboveWinnerName;
playerNames[aboveWinnerIndex] = winnerName;
UpdatePlayerList();
}
C#
void SortPlayers(string winnerName)
{
int winnerIndex = playerNames.IndexOf(winnerName);
int aboveWinnerIndex = winnerIndex - 1;
string aboveWinnerName = playerNames[aboveWinnerIndex];
while (aboveWinnerIndex > 0 && playerWins[winnerName] > playerWins[aboveWinnerName])
{
aboveWinnerIndex--;
aboveWinnerName = playerNames[aboveWinnerIndex];
}
playerNames[winnerIndex] = aboveWinnerName;
playerNames[aboveWinnerIndex] = winnerName;
UpdatePlayerList();
}
I got this now but it always puts it at the top Stefan helped me and it works now

Did you find this page helpful?