C# List<T> OrderBy 字母顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/188141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List<T> OrderBy Alphabetical Order
提问by SaaS Developer
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?
我在 Framework 3.5 上使用 C#。我正在寻找快速排序 Generic List<T>
。在这个例子中,假设我有一个Person
类型为 lastname的 List 。我将如何使用 lambda 表达式对这个 List 进行排序?
List<Person> people = PopulateList();
people.OrderBy(???? => ?????)
采纳答案by Marc Gravell
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
回答by Danimal
people.OrderBy(person => person.lastname).ToList();
回答by Jon Skeet
Do you need the list to be sorted in place, or just an ordered sequence of the contents of the list? The latter is easier:
您是否需要将列表就地排序,或者只是列表内容的有序序列?后者更容易:
var peopleInOrder = people.OrderBy(person => person.LastName);
To sort in place, you'd need an IComparer<Person>
or a Comparison<Person>
. For that, you may wish to consider ProjectionComparer
in MiscUtil.
要就地排序,您需要一个IComparer<Person>
或一个Comparison<Person>
. 为此,您可能希望ProjectionComparer
在MiscUtil 中考虑。
(I know I keep bringing MiscUtil up - it just keeps being relevant...)
(我知道我一直在提出 MiscUtil - 它一直是相关的......)
回答by Bruno
private void SortGridGenerico< T >(
ref List< T > lista
, SortDirection sort
, string propriedadeAOrdenar)
{
if (!string.IsNullOrEmpty(propriedadeAOrdenar)
&& lista != null
&& lista.Count > 0)
{
Type t = lista[0].GetType();
if (sort == SortDirection.Ascending)
{
lista = lista.OrderBy(
a => t.InvokeMember(
propriedadeAOrdenar
, System.Reflection.BindingFlags.GetProperty
, null
, a
, null
)
).ToList();
}
else
{
lista = lista.OrderByDescending(
a => t.InvokeMember(
propriedadeAOrdenar
, System.Reflection.BindingFlags.GetProperty
, null
, a
, null
)
).ToList();
}
}
}
回答by Iman
for me this useful dummy guide - Sorting in Generic List -worked. it helps you to understand 4 ways(overloads) to do this job with very complete and clear explanations and simple examples
对我来说,这个有用的虚拟指南 - 在通用列表中排序 -有效。它通过非常完整和清晰的解释和简单的示例帮助您理解完成这项工作的 4 种方法(重载)
- List.Sort ()
- List.Sort (Generic Comparison)
- List.Sort (Generic IComparer)
- List.Sort (Int32, Int32, Generic IComparer)
- List.Sort()
- List.Sort(通用比较)
- List.Sort(通用 IComparer)
- List.Sort (Int32, Int32, Generic IComparer)
回答by vampire203
you can use linq :) using :
您可以使用 linq :) 使用:
System.linq;
var newList = people.OrderBy(x=>x.Name).ToList();
回答by AnshuMan SrivAstav
You can use this code snippet:
您可以使用此代码片段:
var New1 = EmpList.OrderBy(z => z.Age).ToList();
where New1
is a List<Employee>
.
哪里New1
是List<Employee>
.
EmpList
is variable of a List<Employee>
.
EmpList
是 a 的变量List<Employee>
。
z
is a variable of Employee
type.
z
是一个Employee
类型的变量。
回答by howserss
This is a generic sorter. Called with the switch below.
这是一个通用的分拣机。用下面的开关调用。
dvm.PagePermissions is a property on my ViewModel of type List T in this case T is a EF6 model class called page_permission.
dvm.PagePermissions 是我的 List T 类型的 ViewModel 上的一个属性,在这种情况下,T 是一个名为 page_permission 的 EF6 模型类。
dvm.UserNameSortDir is a string property on the viewmodel that holds the next sort direction. The one that is actaully used in the view.
dvm.UserNameSortDir 是视图模型上的字符串属性,用于保存下一个排序方向。在视图中实际使用的那个。
switch (sortColumn)
{
case "user_name":
dvm.PagePermissions = Sort(dvm.PagePermissions, p => p.user_name, ref sortDir);
dvm.UserNameSortDir = sortDir;
break;
case "role_name":
dvm.PagePermissions = Sort(dvm.PagePermissions, p => p.role_name, ref sortDir);
dvm.RoleNameSortDir = sortDir;
break;
case "page_name":
dvm.PagePermissions = Sort(dvm.PagePermissions, p => p.page_name, ref sortDir);
dvm.PageNameSortDir = sortDir;
break;
}
public List<T> Sort<T,TKey>(List<T> list, Func<T, TKey> sorter, ref string direction)
{
if (direction == "asc")
{
list = list.OrderBy(sorter).ToList();
direction = "desc";
}
else
{
list = list.OrderByDescending(sorter).ToList();
direction = "asc";
}
return list;
}
回答by rosselder83
You can also use
你也可以使用
model.People = model.People.OrderBy(x => x.Name).ToList();