如何对iList进行排序(有无linq) [英] How to sort an iList (With linq or without)

查看:98
本文介绍了如何对iList进行排序(有无linq)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法,我需要对传递给它的iList对象进行排序(在此方法内部).我已经尝试过linq,但是由于它是一个接口,因此出现错误.

在此先感谢

 私有  void  AddListToTree(ComponentArt.Web.UI.TreeView treeView,IList列表)
        {
    // 需要在此处排序列表
    } 

解决方案

http://geekswithblogs.net/paulwhitblog/archive/2006/05/08/77581.aspx [ http://msdn.microsoft.com/en-us/library/5y536ey6. aspx [^ ].引入泛型后,非泛型类型早于.NET Framework v.2.0时就已过时,因此在新开发中绝对不需要使用非泛型非专业集合类型.

如果您同意这样做而不是进行较大的重新设计(我坚持认为更好),那么您的想法是:您应该创建一个临时集合,填充IList实例中的内容,对其进行排序,然后获取适当的实例.支持IList的对象,并使用它代替未排序的IList实例,您应该将其保留完整.怎么样?

这是一种实现方法:用int键填充System.Collections.Generic.SortedList<TKey, TValue>的新实例.它将始终进行排序,请参见 http://msdn.microsoft.com/en-us/library/ms132319 .aspx [^ ] .如果需要控制顺序,请将其构造函数与IComparer<TKey>参数一起使用,请参见上面的链接.以其属性Values为实现IList<TValue>的集合类型.至此,您将完成往返过程:从IList<TValue>实例开始,最后获得了IList<TValue>的另一个实例,这次按照您的比较标准进行了排序.使用它可以在子节点中填充树节点.问题解决了.

—SA


I have the following method and I need to sort the iList object that is being passed to it (inside this method). I have tried linq but since it''s an interface I''m getting errors.

Thanks in advance

private void AddListToTree(ComponentArt.Web.UI.TreeView treeView, IList list)
        {
    //NEED TO SORT THE LIST HERE
    }

解决方案

http://geekswithblogs.net/paulwhitblog/archive/2006/05/08/77581.aspx[^] might help.


It looks like you already get messed your code design and came to the point where you lost the ability to sort items directly as IList does not support this operation. You should do one of the two: either review your design and rethink collection types you use (which is also better) or add a local fix in the created design (which may or may not be acceptable because ultimately you risk having more hacks than codes, which happens all the time). You could also combine the two approaches.

First of all, your should use generic versions of collection interfaces and classes. Use System.Collections.Generic.IList<T> instead, http://msdn.microsoft.com/en-us/library/5y536ey6.aspx[^]. Non-generic types are rendered obsolete as earlier as of .NET Framework v.2.0 when generics were introduced, so there is absolutely no need to use non-generic non-specialized collection types in new development.

If you agree to do the trick instead of big re-design (which I insist is better), the idea is: you should create a temporary collection, populate if from your instance of IList, sort it, get appropriate instance of the object supporting IList and use it instead of your non-sorted instance of IList which you should leave intact. How about it?

Here is one way to do it: populate a new instance of System.Collections.Generic.SortedList<TKey, TValue> with int key. It will be always sorted, see http://msdn.microsoft.com/en-us/library/ms132319.aspx[^]. If you need to control order, use its constructor with IComparer<TKey> argument, see the above link. Take its property Values which is of the collection type implementing IList<TValue>. At this point, you will complete the round trip: you started with the instance of IList<TValue> and finally obtained another instance of IList<TValue>, this time sorted to your comparison criterion. Use it to populate your tree node with children. Problem solved.

—SA


这篇关于如何对iList进行排序(有无linq)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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