如何排序列表<>由存储在结构我的名单℃的整数;>持有 [英] How to Sort a List<> by a Integer stored in the struct my List<> holds
问题描述
我需要排序为我的比赛,我已经写了一个高分文件。
I need to sort a highscore file for my game I've written.
每个排行榜都有一个名字,分数与日期变量。我店每一个在列表中。
Each highscore has a Name, Score and Date variable. I store each one in a List.
下面是一个包含每个高分的数据结构。
Here is the struct that holds each highscores data.
struct Highscore
{
public string Name;
public int Score;
public string Date;
public string DataAsString()
{
return Name + "," + Score.ToString() + "," + Date;
}
}
因此,如何将我按类型排序榜的名单?列表中的每个对象的得分变
So how would I sort a List of type Highscores by the score variable of each object in the list?
任何帮助表示赞赏:D
Any help is appreciated :D
推荐答案
我不知道为什么每个人都提出了基于LINQ的解决方案,将需要额外的内存(特别是因为积分榜是值类型)和了ToList()的调用,如果一个人想重用的结果。最简单的解决方法是使用内置的一个List
I don't know why everyone is proposing LINQ based solutions that would require additional memory (especially since Highscore is a value type) and a call to ToList() if one wants to reuse the result. The simplest solution is to use the built in Sort method of a List
list.Sort((s1, s2) => s1.Score.CompareTo(s2.Score));
这将整理到位的列表中。
This will sort the list in place.
这篇关于如何排序列表<>由存储在结构我的名单℃的整数;>持有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!