csharp 对namevaluecollection进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 对namevaluecollection进行排序相关的知识,希望对你有一定的参考价值。

NameValueCollection col = new NameValueCollection();
col.Add("red", "rouge");
col.Add("green", "verde");
col.Add("blue", "azul");

// order the keys
foreach (var item in col.AllKeys.OrderBy(k => k))
{
    Console.WriteLine("{0}:{1}", item, col[item]);
}

// or convert it to a dictionary and get it as a SortedList
var sortedList = new SortedList(col.AllKeys.ToDictionary(k => k, k => col[k]));
for (int i = 0; i < sortedList.Count; i++)
{
    Console.WriteLine("{0}:{1}", sortedList.GetKey(i), sortedList.GetByIndex(i));
}

// or as a SortedDictionary
var sortedDict = new SortedDictionary<string, string>(col.AllKeys.ToDictionary(k => k, k => col[k]));
foreach (var item in sortedDict)
{
    Console.WriteLine("{0}:{1}", item.Key, item.Value);
}
// https://stackoverflow.com/questions/4101167/sorting-a-namevaluecollection

以上是关于csharp 对namevaluecollection进行排序的主要内容,如果未能解决你的问题,请参考以下文章

csharp 对我们来说更新

csharp C# - 使内部方法对其他程序集可见

csharp 如何使用方法对数字进行平方的示例。

csharp 如何使用方法对数字进行平方的示例。

csharp 对排序输入没有真正有效的LINQ GroupBy()实现

csharp Python中内置的枚举函数可以把一个列表变成索引 - 元素对,这样就可以在对循环中同时迭代索引和元素本身: