排序在C#中一个IList [英] Sorting an IList in C#

查看:159
本文介绍了排序在C#中一个IList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我碰到一个有趣的问题今天来。我们有一个返回一个IList一个WCF Web服务。不是一个真正的大问题,直到我想排序。

So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it.

原来,IList接口没有内置的排序方法。

Turns out the IList interface doesn't have a sort method built in.

我结束了使用 ArrayList.Adapter(列表)的.sort(新MyComparer())的方法来解决问题,但它只是显得有点贫民区来我的。

I ended up using the ArrayList.Adapter(list).Sort(new MyComparer()) method to solve the problem but it just seemed a bit "ghetto" to me.

我玩弄写一个扩展方法,也与自IList继承和实现我自己的sort()方法以及铸造一个名单,但没有这些似乎过于高雅。

I toyed with writing an extension method, also with inheriting from IList and implementing my own Sort() method as well as casting to a List but none of these seemed overly elegant.

所以我的问题是,没有任何人有一个优雅的解决排序一个IList

So my question is, does anyone have an elegant solution to sorting an IList

推荐答案

如何使用LINQ to对象进行排序的吗?

How about using LINQ To Objects to sort for you?

假设你有一个的IList<汽车> ,赛车有一个引擎属性,我相信你可以排序如下:

Say you have a IList<Car>, and the car had an Engine property, I believe you could sort as follows:

from c in list
orderby c.Engine
select c;

编辑:您确实需要快速得到答案在这里了。正如我presented一个稍微不同的语法其他答案,我会离开我的答案 - 但是,其他的答案presented同样有效

这篇关于排序在C#中一个IList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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