如何在c#中对动态列表进行排序 [英] How do I sort the dynamic list in c#

查看:355
本文介绍了如何在c#中对动态列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function void sortDynamicData(typeOfClass,SortKey)
{
List<dynamic> results = null; //results might be of any type it may contain students data or books data or professors data, hence I took as dynamic
results = services.GetMyresults(typeOfClass); //returns list of dynamic objects

results = results.OrderBy(SortKey).ToList();
 ...
 ...
 ...
 
}





我的问题是我想根据sortKey排序结果

任何帮助都会非常明显。



my question is I want to sort the results based on sortKey
Any help would be greatly appreciable.

推荐答案

是的,这个可以使用CompareTo()来完成。



以下链接已经描述了这一点。检查他们是否对您有所帮助



http://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value [ ^ ]

http://www.dotnetperls.com/sort-keyvaluepair [ ^ ]

http://stackoverflow.com/questions/14544953/c-sharp-sorting-a-listkeyvaluepairint-string [ ^ ]



希望这能解决您的问题:)
Yes, this can be done using CompareTo().

Following links have already described this. Check if they help you

http://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value[^]
http://www.dotnetperls.com/sort-keyvaluepair[^]
http://stackoverflow.com/questions/14544953/c-sharp-sorting-a-listkeyvaluepairint-string[^]

Hopefully, this will resolve your problem :)


y你可以使用LINQ

并检查这个网址。

http://stackoverflow.com/questions/5295971/sorting-a-list-with-orderby [ ^ ]


results = results.OrderByDescending(x =>
                    {
                        var dic = x as IDictionary<string, object>;
                        return dic[SortKey];
                    }).ToList();


这篇关于如何在c#中对动态列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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