对列表进行排序<;字符串,int>;统一c#
我想为一些大学的工作做一个排名表。更好的方法是为每个位置创建一个包含字符串、整数的列表。我在文档中搜索:在那里我找到了解决我问题的完美例子,所以我开始工作。现在,我已经创建了Interfece:对列表进行排序<;字符串,int>;统一c#,c#,list,sorting,unity3d,C#,List,Sorting,Unity3d,我想为一些大学的工作做一个排名表。更好的方法是为每个位置创建一个包含字符串、整数的列表。我在文档中搜索:在那里我找到了解决我问题的完美例子,所以我开始工作。现在,我已经创建了Interfece: public class Name_Puntuation: IComparable public string namePlayer; public int puntuation; public Name_Puntuation( string newNomPlayer, int newPuntuat
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;
}
我还向GameController添加了我想对列表执行的操作。此类控制游戏的画布。我们将所有信息打印到文本字段,因此我想处理排名:
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);
}
}
}
公共类游戏控制器:单行为{
公开名单排名;
无效开始(){
playerName=GetComponent().inp;//所选播放器的名称
排名=新列表();
}
无效更新(){}
公共空间排名(){
int puntuation=punts;
添加(新名称_Puntuation(playerName,Puntuation));
排序();
foreach(排名中的Nom_Puntuacio p){
打印(p.playerName+“”+p.puntuation);
}
}
}
但现在我被卡住了。我想在游戏结束后将这些信息添加到列表中。我什么时候必须调用该函数?rankig.sort();将对每个玩家的puntuation(整数)进行排序?最后一个问题,如何重新启动创建新播放器的过程?
任何建议都会很有帮助
谢谢,Marco如果我理解正确的话,您需要排序“每个玩家的puntuation(integer)”,所以可能解决方案是使用
ranking = ranking.OrderBy(x => x.puntuation).ToList();
或
而不是:
ranking.Sort();
如果我错了,请纠正我
ranking.Sort();