如何对 iList 进行排序(使用或不使用 linq)

发布于 2024-12-18 08:16:05 字数 713 浏览 3 评论 0原文

可能的重复:
在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小糖芽 2024-12-25 08:16:05

您应该使用 IList 的通用形式才能使用 LINQ 扩展方法:

private void AddListToTree<T>(ComponentArt.Web.UI.TreeView treeView,
                              IList<T> list)
{
    var orderedList = list.OrderBy(t => t);
    // ...
}

如果您无法修改方法的签名,但您知道 IList 中对象的类型代码>,您可以使用Cast

private void AddListToTree(ComponentArt.Web.UI.TreeView treeView,
                           IList list)
{
    var orderedList = list.Cast<SomeType>().OrderBy(x => x);
    // ...
}

You should use the generic form of IList to be able to use the LINQ extension methods:

private void AddListToTree<T>(ComponentArt.Web.UI.TreeView treeView,
                              IList<T> list)
{
    var orderedList = list.OrderBy(t => t);
    // ...
}

If you can't modify the method's signature but you know the type of the objects in the IList, you can use Cast:

private void AddListToTree(ComponentArt.Web.UI.TreeView treeView,
                           IList list)
{
    var orderedList = list.Cast<SomeType>().OrderBy(x => x);
    // ...
}
<逆流佳人身旁 2024-12-25 08:16:05

对于 LINQ:
重复的问题为: Sorting an IList in C#

您还可以使用扩展方法将排序命令添加到 IList: 为什么没有对 IList进行排序?!?! (已编辑)'

您可以执行以下操作(我不赞同,但仍然可行)

IList list;
ArrayList al = new ArrayList(list);
al.Sort();

您还可以利用 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)

IList list;
ArrayList al = new ArrayList(list);
al.Sort();

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

帅哥哥的热头脑 2024-12-25 08:16:05

您可以使用 Cast() 将其更改为 IList,然后使用 OrderBy()

private void AddListToTree<T>(ComponentArt.Web.UI.TreeView treeView, IList list)
{
    var ordered = list.Cast<T>().OrderBy(e => e);
}

您需要知道的地方进行排序的类型。

You could use Cast<T>() to change it to IList<T> then use OrderBy():

private void AddListToTree<T>(ComponentArt.Web.UI.TreeView treeView, IList list)
{
    var ordered = list.Cast<T>().OrderBy(e => e);
}

Somewhere you will need to know the type to do the sorting.

陈独秀 2024-12-25 08:16:05

一种方法(请注意,您必须在 from 子句中指定项目类型):

IList list = new ArrayList();
list.Add("z");
list.Add("a");
list.Add("c");

IEnumerable<string> ordered =
    from string item in list
    orderby item
    select item;

foreach (var s in ordered)
{
    Console.WriteLine(s);
}

打印:

a
c
z

注意:基于您后来的评论

它们可以是 3 个不同类别的类型

这是行不通的。我会认真考虑重构调用代码。

如果您无法编辑类型,并且类型不应在排序列表中混合,则可以选择以下选项:

var one = list.OfType<TypeOne>().OrderBy(x => x.Id);
var two = list.OfType<TypeTwo>().OrderBy(x => x.Name);
var three = list.OfType<TypeThree>().OrderBy(x => x.Nom);

var result =
    one.Cast<object>()
    .Concat(two.Cast<object>())
    .Concat(three.Cast<object>());

One way (note that you must specify the item type in the from clause):

IList list = new ArrayList();
list.Add("z");
list.Add("a");
list.Add("c");

IEnumerable<string> ordered =
    from string item in list
    orderby item
    select item;

foreach (var s in ordered)
{
    Console.WriteLine(s);
}

Prints:

a
c
z

Note: Based on your later comment of

they can be type of 3 different classes

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:

var one = list.OfType<TypeOne>().OrderBy(x => x.Id);
var two = list.OfType<TypeTwo>().OrderBy(x => x.Name);
var three = list.OfType<TypeThree>().OrderBy(x => x.Nom);

var result =
    one.Cast<object>()
    .Concat(two.Cast<object>())
    .Concat(three.Cast<object>());
静待花开 2024-12-25 08:16:05

一种选择是使用 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.

无声无音无过去 2024-12-25 08:16:05

如果无法更改签名或使用 LINQ,请使用扩展方法或自定义方法。

@Ryan Ternier 列出的重复问题中给出的示例:

LINQ:重复的问题为:
在 C# 中对 IList 进行排序

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:

LINQ: Duplicate question as :
Sorting an IList in C#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文