Skip to content
Advertisement

2 dimensional associative array sorting with conditional statements in PHP

I’m having trouble figuring out how to solve this question, it’s from a free online test website. Here’s the link: https://www.testdome.com/questions/php/league-table/19939?questionIds=7278,19939&generatorId=30&type=fromtest&testDifficulty=Hard

But to be clearer, I’m writing the question and my answer as well. The starting answer is there in the link written above.

Question:

The LeagueTable class tracks the score of each player in a league. After each game, the player records their score with the recordResult function.

The player’s rank in the league is calculated using the following logic:

The player with the highest score is ranked first (rank 1). The player with the lowest score is ranked last. If two players are tied on score, then the player who has played the fewest games is ranked higher. If two players are tied on score and number of games played, then the player who was first in the list of players is ranked higher. Implement the playerRank function that returns the player at the given rank.

For example:

JavaScript

All players have the same score. However, Arnold and Chris have played fewer games than Mike, and as Chris is before Arnold in the list of players, he is ranked first. Therefore, the code above should display “Chris”.

My code:

JavaScript

Can anybody helps me solve this question please?

Advertisement

Answer

Using usort in your case

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement