Sort Player List by Score

Options
Hello everybody,
I want to display a list of the current players in the room, sorted by how much score each one has. (I'm using PhotonNetwork.LocalPlayer.SetScore to store the score).

How would I do this?
Thanks

Comments

  • I figured it out!

    public Text text;
    public List<Player> playerList;

    private void FixedUpdate()
    {
    UpdateSortList();
    }
    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
    UpdateSortList();
    }
    public override void OnPlayerLeftRoom(Player otherPlayer)
    {
    UpdateSortList();
    }
    public void UpdateSortList()
    {
    text.text = "";
    playerList = PhotonNetwork.PlayerList.ToList();
    playerList.Sort(sortByScore);
    for(int i = 0; i < playerList.Count; i++)
    {
    int rank = i + 1;
    text.text += rank + ". " + playerList.NickName + "\n";
    }
    }
    public static int sortByScore(Player a, Player b)
    {
    return b.GetScore().CompareTo(a.GetScore());
    }