如何对列表进行排序< keyvaluepair>按字母顺序排列 [英] How to sort the list<keyvaluepair> in alphabetical order

查看:107
本文介绍了如何对列表进行排序< keyvaluepair>按字母顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表

List<KeyValuePair<int, NoNexusCounterparty>> listcps = noNexusCpsdict.ToList();



NoNexusCounterparty是一个包含交易对手列表的类对象。

如何按字母顺序对此列表进行排序。



我的尝试:



listcps = listcps.Sort ();



无效..


NoNexusCounterparty is a class object which contains list of counterparties.
How to sort this list by alphabetical order.

What I have tried:

listcps = listcps.Sort();

not working..

推荐答案

尝试使用OrderBy:

Try using OrderBy:
List<KeyValuePair<int, string>> myList = new List<KeyValuePair<int, string>>();
myList.Add(new KeyValuePair<int,string>(9, "Nine"));
myList.Add(new KeyValuePair<int,string>(1, "One"));
myList.Add(new KeyValuePair<int,string>(3, "Three"));
myList = myList.OrderBy(kvp => kvp.Key).ToList();
foreach (var kvp in myList) Console.WriteLine("{0}:{1}", kvp.Key, kvp.Value);
myList = myList.OrderBy(kvp => kvp.Value).ToList();
foreach (var kvp in myList) Console.WriteLine("{0}:{1}", kvp.Key, kvp.Value);



你会得到:


You will get:

1:One
3:Three
9:Nine



And

9:Nine
1:One
3:Three


你的意思是不工作?请查看下面的代码段

What you mean by Not working ? check out below snippet
var New1 = EmpList.OrderBy(z => z.Age).ToList();



其中New1是List< employee> ;.

EmpList是List< employee>的变量。

z是Employee类型的变量。


where New1 is a List<employee>.
EmpList is variable of a List<employee>.
z is a variable of Employee type.


这篇关于如何对列表进行排序&lt; keyvaluepair&gt;按字母顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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