I've written following code and it works too - but I wanted to know whether their is better way than this :
NameValueCollection optionInfoList = ..... ;
if (aSorting)
{
optionInfoListSorted = new nameValueCollection();
String[] sortedKeys = optionInfoList.AllKeys;
Array.Sort(sortedKeys);
foreach (String key in sortedKeys)
optionInfoListSorted.Add(key, optionInfoList[key]);
return optionInfoListSorted;
}
Use a SortedDictionary instead.
Perhaps you could use a different kind of list, that supports sorting directly?
List<KeyValuePair<string, string>> optionInfoList = ...;
if (sorting) {
optionInfoList.Sort((x,y) => String.Compare(x.Key, y.Key));
}
return optionInfoList;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With