Sorting a List {string, int}
Hello guys,
i'm trying to make a rankingtable for some university work. The better way that came in mind was creating a List with String, Integer for each position. I searched in the documentation : https://unity3d.com/es/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries?gq=sort where i found the perfect example for my problem, so i started working. Now, i've created the Interfece: public class Name_Puntuation: IComparable {
public string namePlayer;
public int puntuation;
public Name_Puntuation( string newNomjuador, int newPuntuacio ){
nomPlayer = newnomPlayerr;
puntuation= newPuntuation;
}
public int CompareTo(Nom_Puntuation other){
if (other == null) {
return 1;
}
return Puntuation - other.Puntuation;
}
}
I also added to my GameController what i want to do with the list. This class controlls the canvas of the game. We print all the information to textfields, and so i wanted to do with the ranking:
public class GameController : MonoBehaviour {
public List <Name_Puntuation> ranking;
void Start () {
playerName= GetComponent<MenuController> ().inp; //name the player choosed
ranking = new List<Nom_Puntuacio> ();
}
void Update () {
}
public void Ranking(){
int puntuation= punts;
ranking.Add (new Name_Puntuation(playerName, puntuation));
ranking.Sort ();
foreach (Nom_Puntuacio p in ranking) {
print (p.playerName+ " " + p.puntuation);
}
}
}
but now I'm stuck. I want to add the information to the list when the game is finnished. When do i have to call the function? rankig.sort(); will sort the puntuation (integer) of each player? Thanks
This might be a question better suited for Stack Overflow
Hello, I have asked the question in StackOverflow as you suggested. Here's the link,it may be usefull for someone: https://stackoverflow.com/questions/54266825/sorting-a-list-string-int-in-unity-c-sharp
Your answer
Follow this Question
Related Questions
How can I check value stored in a list when pressing button with the same name? 2 Answers
Can Static classes have static lists? 1 Answer
How can I sort a list of GameObject by name? 1 Answer
List resulting in out of range 0 Answers
How to know if a list contains GameObjects with a specific Varaible values ? 0 Answers