Sort an array to make an highscore

Hi, I want to sort an int array and I know that a LOT of people have already asked this question, but the anwser is always Array.Sort()
The problem with using Array.Sort() is that I don’t know what player had which score after sorting
For example :
score[0] = 1 ← Player one’s score
score[1] = 3 ← Player two’s score
score[2] = 2 ← Player three’s score

If I use Array.Sort, player one will get player two’s score and player two will get player three’s score
Is there any way to sort the score of each player and still know what score have each player ?

So you need to extend your data so you store the name of the player and its score:

public class Player
{
      public string Name { get ;  private set; }
      public int Score { get; private set; }
      public Player(string name, int score)
      {
              if(string.IsNullOrEmpty(name) == true){ throw new Exception(); }
              if(score < 0) { throw new Exception(); }
              this.Name = name;
              this.Score = score;
      }
}

With this, you can store all your player in an array of Player and finally use Array.Sort

Player [] players = new Player[]
{
      new Player("Jeff", 20);
      new Player("Tim", 15);
      new Player("Donald", 0);
};
Array.Sort(players, (player1, player2)=>
{
     return player1.Score.CompareTo(player2.Score);
});

You can simplify the process by making your Player as IComparable:

public class Player : IComparable
{
      public string Name { get ;  private set; }
      public int Score { get; private set; }
      public Player(string name, int score)
      {
              if(string.IsNullOrEmpty(name) == true){ throw new Exception(); }
              if(score < 0) { throw new Exception(); }
              this.Name = name;
              this.Score = score;
      }

      public int CompareTo(object obj)
      {
            if (obj is User) {
                return this.Score.CompareTo((obj as Player).Score);  
            }
            throw new ArgumentException("Object is not a Player");
       }
}

with this you can simply do :

    Array.Sort(players);

The compiler knows what to compare and will sort accordingly.
Your array is now ordered by score with smallest first and greatest last, if you want greater first, you can Reverse the array, or invert the order in the compare to method.