本文介绍了分选的NameValueCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么排序按字母顺序的NameValueCollection?我必须首先转换到另一个列表,如排序列表或IList的什么?如果那怎么办呢?现在我有我在的namevalucollection变量的所有字符串。

How do I sort a namevaluecollection in alphabetical order? Do I have to cast it to another list first like the sorted list or Ilist or something? If then how do I do that? right now I have all my string in the the namevalucollection variable.

推荐答案

preferably使用合适的集合开始,如果它在你的手中。但是,如果你要对的NameValueCollection 这里的工作有一些不同的选项:

Preferably use a suitable collection to begin with if it's in your hands. However, if you have to operate on the NameValueCollection here are some different options:

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);
}

这篇关于分选的NameValueCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:19