如何对 iList 进行排序(使用或不使用 linq)
可能的重复:
在 C# 中对 IList 进行排序
我有以下方法,我需要对传递给它的 iList 对象(在此方法内)。我尝试过 linq 但由于它是一个接口,所以我遇到了错误。
提前致谢
private void AddListToTree(ComponentArt.Web.UI.TreeView treeView, IList list)
{
//NEED TO SORT THE LIST HERE
}
请注意,我的类型是动态的。
我认为我应该创建一个临时集合,从我的 IList 实例中填充 if ,对其进行排序,获取支持 IList 的对象的适当实例并使用它,而不是我应该保持原样的未排序的 IList 实例。 所以我尝试获取如下类型:
Type[] listTypes = list.GetType().GetGenericArguments();
Type listType = null;
if (listTypes.Length > 0)
{
listType = listTypes[0];
}
但我无法使用此类型创建新列表
Possible Duplicate:
Sorting an IList in C#
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
}
Please note that my type is Dynamic.
I think I should create a temporary collection, populate if from my instance of IList, sort it, get appropriate instance of the object supporting IList and use it instead of my non-sorted instance of IList which I should leave intact.
So I tried getting the type like following:
Type[] listTypes = list.GetType().GetGenericArguments();
Type listType = null;
if (listTypes.Length > 0)
{
listType = listTypes[0];
}
But I cannot create a new List with this type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该使用
IList
的通用形式才能使用 LINQ 扩展方法:如果您无法修改方法的签名,但您知道
IList
中对象的类型代码>,您可以使用Cast
:You should use the generic form of
IList
to be able to use the LINQ extension methods:If you can't modify the method's signature but you know the type of the objects in the
IList
, you can useCast
:对于 LINQ:
重复的问题为: Sorting an IList in C#
您还可以使用扩展方法将排序命令添加到 IList: 为什么没有对 IList进行排序?!?! (已编辑) '
您可以执行以下操作(我不赞同,但仍然可行)
您还可以利用 IComparable:
http://devpinoy.org/blogs/keithrull/archive/2007/03/23/sorting-a-generic-list-of-object-in-c-using-icomparable-and-anonymous-delegates.aspx
或
http://foxsys.blogspot.com/2007/06/how-to-sort-generic-ilist.html
For LINQ:
Duplicate question as : Sorting an IList in C#
You can also use extension methods to add the Sort command to IList: Why is there no Sort for IList<T>?!?! (edited)'
You can do the following (WhicH I don't endorse, but it's still doable)
You can also utilize IComparable:
http://devpinoy.org/blogs/keithrull/archive/2007/03/23/sorting-a-generic-list-of-object-in-c-using-icomparable-and-anonymous-delegates.aspx
or
http://foxsys.blogspot.com/2007/06/how-to-sort-generic-ilist.html
您可以使用
Cast()
将其更改为IList
,然后使用OrderBy()
:您需要知道的地方进行排序的类型。
You could use
Cast<T>()
to change it toIList<T>
then useOrderBy()
:Somewhere you will need to know the type to do the sorting.
一种方法(请注意,您必须在 from 子句中指定项目类型):
打印:
注意:基于您后来的评论
这是行不通的。我会认真考虑重构调用代码。
如果您无法编辑类型,并且类型不应在排序列表中混合,则可以选择以下选项:
One way (note that you must specify the item type in the from clause):
Prints:
Note: Based on your later comment of
this will not work. I would seriously consider refactoring the calling code.
If you have no ability to edit the types, and if the types should not be intermixed in the sorted list, here's an option:
一种选择是使用 ArrayList 在 IList 周围创建 ArrayList 适配器.Adapter,然后使用 ArrayList.Sort。这是如果您需要就地排序操作来改变数组内容,而不是返回排序的枚举。
One option is to create an ArrayList adapter around the IList using ArrayList.Adapter, then sort it using ArrayList.Sort. This is if you need an in-place sorting operation that mutates the array contents, rather than returning a sorted enumeration.
如果无法更改签名或使用 LINQ,请使用扩展方法或自定义方法。
@Ryan Ternier 列出的重复问题中给出的示例:
If you can't change the signature or use LINQ, use an extension method or custom method.
Examples given in the duplicate question listed by @Ryan Ternier: