如何对 IList进行排序?
IList
没有 Sort()
函数。有人可以帮我解决这个问题吗? 我想对我自己的 IList 进行排序。
假设这是我的 IList:
public class MyObject()
{
public int number { get; set; }
public string marker { get; set; }
}
如何使用标记字符串对 myobj 进行排序?
public void SortObject()
{
IList<MyObject> myobj = new List<MyObject>();
}
There's no Sort()
function for IList
. Can someoene help me with this?
I want to sort my own IList.
Suppose this is my IList:
public class MyObject()
{
public int number { get; set; }
public string marker { get; set; }
}
How do I sort myobj using the marker string?
public void SortObject()
{
IList<MyObject> myobj = new List<MyObject>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
OrderBy
示例
作为案例不敏感你应该使用 IComparer
Use
OrderBy
Example
For a case insensitive you should use a IComparer
我会反对使用
OrderBy
与列表,因为它是一个 LINQ 扩展方法,因此:ToList()
时,它会对其进行迭代并用项目填充另一个新列表。本质上:除了实际排序之外,它还创建并填充 2 个新列表和 2 个可枚举值。
相比之下,
List.Sort()
就地排序并且不创建任何内容,因此效率更高。我的建议是:
List.Sort()
或Array.Sort(array)
I would go against using
OrderBy
with a list because it's a LINQ extension method, therefore:ToList()
, it iterates on it and fills another new list with the items.In essence: it creates and fills 2 new lists and 2 enumerables in addition to the actual sorting.
In comparison,
List.Sort()
sorts in place and create nothing so it's way more efficient.My recommendation would be:
List.Sort()
orArray.Sort(array)
Array.Sort(array)
and return it.OrderBy
肯定可以完成工作,但我个人更喜欢List.Sort
的语法,因为您可以为其提供一个Comparison
委托必须编写一个实现IComparer
的类。我们可以使用扩展方法来实现该目标,如果您对此感兴趣,请查看 SortExtensions:http://blog.velir.com/index.php/2011/02/17/ilistt-sorting-a-better-way/
OrderBy
definitely gets the job done, but I personally prefer the syntax ofList.Sort
because you can feed it aComparison<T>
delegate instead of having to write a class that implementsIComparer<T>
. We can accomplish that goal with an extension method, and if that's something you're interested in, check out SortExtensions:http://blog.velir.com/index.php/2011/02/17/ilistt-sorting-a-better-way/
要了解为什么不使用 OrderBy 或类似的信息,请查看 Christophe 的回答。
这是进行快速排序的一种尝试:
For explanation why not to use OrderBy or similar check Christophe's answer.
Here is one attempt to make fast Sort:
要就地排序,您基本上会看到这两种方法:
第二种方法
可能看起来更简单但对于值类型集合来说不太好,因为它会导致装箱惩罚。此外,无法保证您的
IList
将实现IList
。在我看来,第一个更好。您还可以使用第一种方法就地对
ICollection
进行排序,但由于ICollection
合约不保证订单(想想哈希结构)。无论如何,向您展示代码示例:关于排序稳定性的说明,.NET 的数组/列表排序算法不稳定。 对于稳定的排序,您必须使用:
这不可能像不稳定的排序那么快。
最后,为了完整的答案,也许watbywbarif 采用的复合方法更好:
这就是内置方法。为了更快地实施,您必须推出自己的,请参阅:https://stackoverflow.com/a/19167475
To sort in-place you would essentially see these two approaches:
and
The second one might look simpler but won't be great for value type collections since it incur boxing penalties. Furthermore there is no guarantee your
IList<T>
will be implementingIList
. First one is better IMO.You can also use the first approach to sort an
ICollection<T>
in-place but it is questionable if you should expose such a functionality sinceICollection<T>
contract doesn't guarantee an order (think hash structures). Anyway to show you code example:A note on sort stability, .NET's Array/List sorting algorithms are unstable. For a stable sort you will have to use:
This can't be as fast as unstable sorts.
Finally, for a complete answer, perhaps a composite approach taken by watbywbarif is better:
That's as far as built-in approaches go. For faster implemenation you will have to roll out your own, see: https://stackoverflow.com/a/19167475