列表<T>按字母顺序排序 [英] List&lt;T&gt; OrderBy Alphabetical Order

查看:22
本文介绍了列表<T>按字母顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Framework 3.5 上使用 C#.我希望快速对通用 List 进行排序.在这个例子中,假设我有一个 Person 类型的 List,其属性为 lastname.我将如何使用 lambda 表达式对这个 List 进行排序?

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<T>. For the sake of this example, let's say I have a List of a Person type with a property of lastname. How would I sort this List using a lambda expression?

List<Person> people = PopulateList();
people.OrderBy(???? => ?????)

推荐答案

如果您指的是就地排序(即更新列表):

If you mean an in-place sort (i.e. the list is updated):

people.Sort((x, y) => string.Compare(x.LastName, y.LastName));

如果您的意思是新列表:

If you mean a new list:

var newList = people.OrderBy(x=>x.LastName).ToList(); // ToList optional

这篇关于列表<T>按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆