[英]Sorting a List <string, int> in Unity c#
我正在嘗試為某些大學工作制定一個排名表。 想到的更好的方法是為每個位置創建一個帶字符串,整數的列表。 我在文檔中進行了搜索: https : //unity3d.com/es/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries?gq=sort ,在其中找到了解決問題的最佳示例,因此我開始工作。 現在,我創建了Interfece:
public class Name_Puntuation: IComparable
public string namePlayer;
public int puntuation;
public Name_Puntuation( string newNomPlayer, int newPuntuation){
nomPlayer = newNomPlayer;
puntuation= newPuntuation;
}
public int CompareTo(Name_Puntuation other){
if (other == null) {
return 1;
}
return Puntuation - other.Puntuation;
}
我還向列表添加了我想做的事情。 此類控制游戲的畫布。 我們將所有信息打印到文本字段,因此我想對排名進行處理:
public class GameController : MonoBehaviour {
public List <Name_Puntuation> ranking;
void Start () {
playerName= GetComponent<MenuController> ().inp; //name the player choosed
ranking = new List<Name_Puntuation> ();
}
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);
}
}
}
但是現在我被卡住了。 我想在游戲結束時將信息添加到列表中。 我什么時候必須調用該函數? rankig.sort(); 將排序每個玩家的點數(整數)? 最后一個問題,我該如何重新啟動流程以創建新播放器? 任何建議將非常有幫助。
謝謝,Marco
如果我理解正確,您想對“每個玩家的小數(整數)”進行排序,那么解決方案可能是使用
ranking = ranking.OrderBy(x => x.puntuation).ToList();
要么
ranking = ranking.OrderByDescending(x => x.puntuation).ToList();
代替:
ranking.Sort();
如果我錯了,請糾正我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.