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

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

问题描述

我在Framework 3.5上使用C#.我正在寻找对通用List<T>进行快速排序的方法.就本例而言,假设我有一个Person类型的列表,其具有姓氏的属性.我将如何使用Lambda表达式对该列表进行排序?

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

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

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