In my Race Game, I am using a waypoint-based position tracking system. I am using an ordered list. Below is my code and it's working fine.
IOrderedEnumerable<KeyValuePair<string, Player>> sortedPlayer = players.OrderBy(x => x.Value.distanceToWaypoint).OrderByDescending(x => x.Value.activeWaypointIndex);
My problem is after the first lap it's acting wired. it's not working fine. means consider that the game has 10 waypoints. after the player passed 10 waypoints to the first way point its starts acting wired.
I have a lap counter script that counts how many laps passed correctly. is that any way to fix this problem using that lap count
In general you probably rather want to use ThenBy
and ThenByDescending
so the new query doesn't completely overrule the sorting you already did with the previous call but rather only uses the next sorting if with the previous sorting two or more elements would be in an equal spot.
And then the sorting order should be
Completed Laps (descending)
If one player has already more laps completed as another one then the other two categories don't matter at all. You already know that this player is more ahead.
active waypoint (descending)
If two players have the same amount of completed laps but different active waypoints then you know that the player with a higher waypoint index already has to be more ahead.
distance to active waypoint
And finally if two players have the same amount of completed laps and the same active waypoint index then the player with the smaller distance is more ahead.
So your code should probably rather be something like e.g.
var sortedPlayer = players
.OrderByDescending(x => x.Value.amountOfCompletedLaps)
.ThenByDescending(x => x.Value.activeWaypointIndex)
.ThenBy(x => x.Value.distanceToWaypoint);