C# 使用 OrderBy<> 对项目数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/620534/
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
Sort array of items using OrderBy<>
提问by Zooking
I have an array of items and I would like to sort on one of their properties. I can access the items property using "item.Fields["FieldName"].Value" the property is returned as a string but I can cast it to an int.
我有一系列项目,我想对它们的一个属性进行排序。我可以使用“ item.Fields["FieldName"].Value访问 items 属性,该属性作为字符串返回,但我可以将其转换为 int。
I had a look at OrderBy<> but I have no idea of how to use it.
我看过 OrderBy<> 但我不知道如何使用它。
采纳答案by Jon Skeet
To be clear, OrderBy
won't sort the array in place - it will return a new sequence which is a sorted copyof the array. If that's okay, then you want somethinglike:
需要明确的是,OrderBy
不会对数组进行原地排序 - 它会返回一个新序列,它是数组的排序副本。如果没问题,那么你想要这样的东西:
var sorted = array.OrderBy(item => item.Fields["FieldName"].Value);
On the other hand, I don't understand your comment that the property is returned as a string but that you can cast it to an int - you can't cast strings to ints, you have to parse them. If that's what you meant, you probably want:
另一方面,我不明白您的评论,即该属性作为字符串返回,但您可以将其转换为 int - 您不能将字符串转换为 int,您必须解析它们。如果这就是你的意思,你可能想要:
var sorted = array.OrderBy(item => int.Parse(item.Fields["FieldName"].Value));
If you want that as an array, you can call ToArray()
afterwards:
如果你想把它作为一个数组,你可以在ToArray()
之后调用:
var sorted = array.OrderBy(item => int.Parse(item.Fields["FieldName"].Value))
.ToArray();
Alternatively you could use Array.Sort
if you want to sort in-place, but that will be somewhat messier.
或者,Array.Sort
如果您想就地排序,也可以使用,但这会有些混乱。
回答by Quintin Robinson
If you can use orderby it should be easy, try the following. I threw in the int.Parse although depending on how you actually want to sort this might not be required.
如果您可以使用 orderby 应该很容易,请尝试以下操作。我投入了 int.Parse 虽然这取决于您实际想要的排序方式,但可能不需要。
var sorted = array.OrderBy(item => int.Parse(item.Fields["FieldName"].Value));
回答by andleer
var sortedArray = items.OrderBy(i => i.property).ToArray();
If you don't want an array, you can leave that off in which case you will have an IEnumerable<> of type item.
如果你不想要一个数组,你可以不使用它,在这种情况下你将拥有一个 IEnumerable<> 类型的 item。
回答by Guffa
Use the Sort method to sort an array:
使用 Sort 方法对数组进行排序:
Array.Sort(theArray, (a, b) => String.Compare(a.Fields["FieldName"].Value, b.Fields["FieldName"].Value));
If you are not using C# 3, you use a delegate instead of a lambda expression:
如果您不使用 C# 3,则使用委托而不是 lambda 表达式:
Array.Sort(theArray, delegate(Item a, Item b) { return String.Compare(a.Fields["FieldName"].Value, b.Fields["FieldName"].Value); } );
(This also works with framework 2, which the OrderBy extension doesn't.)
(这也适用于框架 2,而 OrderBy 扩展不适用。)
回答by Wenhan Kong
It's worth mentioning that List<T>.Sort
is based on quick sort, and in most cases it is not a stable sort.
值得一提的是,它List<T>.Sort
是基于快速排序的,在大多数情况下它不是一个稳定的排序。
This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.
此实现执行不稳定的排序;也就是说,如果两个元素相等,它们的顺序可能不会被保留。相反,稳定排序保留相等元素的顺序。
However you can use Enumberable.OrderBy
which performs a stable sort.
但是,您可以使用Enumberable.OrderBy
which 执行稳定排序。
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.
此方法执行稳定排序;也就是说,如果两个元素的键相等,则保留元素的顺序。相反,不稳定排序不会保留具有相同键的元素的顺序。