开发者

Sorting a list in C#

I have a class that looks like this:

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; }
}

And a list of this class:

List<Record> Records = new List<Record>();

This list contains a number of entries. How can I sort this list on the basis of Record.host (alphabetically) ?

Is there a built-in function, or would I need to write my own? If so, c开发者_开发问答ould someone point me in the right direction by perhaps giving me some pseudo-code?


You can sort them using Linq if you just need to iterate them in sorted order.

Records.OrderBy(r => r.host)

Otherwise, you could call:

Records.sort((x, y) => string.Compare(x.host, y.host)); 

To permanently sort the list.


The following will sort the list itself:

Records.Sort((a, b) => String.Compare(a.host, b.host));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜