我有一个看起来像这样的类:
class Record
{
public string host { get; set; }
public string type { get; set; }
public string name { get; set; }
public string RunAsUser { get; set; }
public string Status { get; set; }
public string StartMode { get; set; }
}
以及这个类的列表:
List<Record> Records = new List<Record>();
此列表包含许多条目。我如何根据 Record.host
(按字母顺序)对该列表进行排序?
是否有内置函数,还是我需要自己编写?如果是这样,有人可以通过给我一些伪代码来为我指明正确的方向吗?
最佳答案
如果您只需要按排序顺序迭代它们,您可以使用 Linq 对它们进行排序。
Records.OrderBy(r => r.host)
否则,您可以调用:
Records.sort((x, y) => string.Compare(x.host, y.host));
永久排序列表。
关于c# - 在 C# 中对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4207870/