Sort a List with a public class
Hello I Have to sort a list with two properties playerName and score. Thanks in advice.
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class ManageListLeaderboard : IComparable<ManageListLeaderboard> { public string playerName; public int score; %|1830307367_3|% %|-714131633_4|% score = newScore; } public int CompareTo (ManageListLeaderboard other) { if (other == null) { return 1; } return score - other.score; %|-212554372_13|% }
And this is the manage script of the level.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class MultiplayerScript : Photon.MonoBehaviour {
//---------------------------------------- SCORE PLAYERS ---------------------------------
public Text [] scorePlayerText;
public List<ManageListLeaderboard> manageLeaderboard;
public Text[] LeaderBoardText;
public int[] score;
public GameObject leaderboardPanel;
public bool call = false;
//-----------------------------------------------------------------------------------------
void Start(){
manageLeaderboard = new List<ManageListLeaderboard> ();
score = new int[8];
}
void Update(){
scorePlayerText [0].text = score [0].ToString ();
scorePlayerText [1].text = score [1].ToString ();
scorePlayerText [2].text = score [2].ToString ();
scorePlayerText [3].text = score [3].ToString ();
scorePlayerText [4].text = score [4].ToString ();
scorePlayerText [5].text = score [5].ToString ();
scorePlayerText [6].text = score [6].ToString ();
scorePlayerText [7].text = score [7].ToString ();
foreach (ManageListLeaderboard man in manageLeaderboard) {
print (man.playerName + " " + man.score);
}
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [0].ToString (), 3));
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [1].ToString (), 5));
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [2].ToString (), 8));
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [3].ToString (), 7));
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [4].ToString (), score [4]));
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [5].ToString (), score [5]));
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [6].ToString (), score [6]));
manageLeaderboard.Add (new ManageListLeaderboard (PhotonNetwork.playerList [7].ToString (), score [7]));
manageLeaderboard.Sort ();
manageLeaderboard.Reverse ();
}
Comment