Because it is convenient to operate under different data types during data processing, I have listed several methods.
[Different method calls under the same namespace: namespace name. class name. method name]
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("aname", "bjson");
dic.Add("url", "http://www.bejson.com");
dic.Add("address", "江苏苏州科技园路");
dic.Add("country", "中国");
#region 升序方法1
//升序方法1:
Dictionary<string, string> ascdic = dic.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value.ToString());//对key进行升序
//遍历元素
foreach (KeyValuePair<string, string> kv in ascdic)
{
Console.WriteLine("方法一升序以后的key列表为:" + kv.Key + ", value为:" + kv.Value);
}
#endregion
#region 升序方法2:直接使用SortedDictionary
//升序方法2:直接使用SortedDictionary
SortedDictionary<string, string> sorDic = new SortedDictionary<string, string>();
sorDic.Add("street", "学院路");
sorDic.Add("city", "上海");
sorDic.Add("cab", "30");
sorDic.Add("url", "http://www.google.com");
//遍历元素
foreach (KeyValuePair<string, string> kv in sorDic)
{
Console.WriteLine("方法2的升序以后顺序为key:" + kv.Key + ", value为:" + kv.Value);
}
#endregion
#region 升序方法3
//升序排序方法2的简单版本
SortedDictionary<string, string> sac = new SortedDictionary<string, string>(dic);
foreach (KeyValuePair<string, string> kv in sorDic)
{
Console.WriteLine("方法2的另一个版本以后顺序为key:" + kv.Key + ", value为:" + kv.Value);
}
#endregion
#region 升序方法4
//升序方法3
ArrayList arrayList = new ArrayList();
arrayList.AddRange(dic.Keys);
arrayList.Sort();
//遍历元素
StringBuilder sbu = new StringBuilder();
Dictionary<string, string> ascDic = new Dictionary<string, string>();
foreach (string al in arrayList)//注意arrayList与al的数据类型是子集关系
{
sbu.Append(al).Append(dic[al]);//得到数据为key1value1key2value2
ascDic.Add(al, dic[al]);
}
foreach (KeyValuePair<string, string> kv in ascDic)
{
Console.WriteLine("方法3的数据封装以后顺序为key:" + kv.Key + ", value为:" + kv.Value);
}
Console.WriteLine("方法3升序以后的顺序为:" + sbu);
#endregion
//降序排列
Console.WriteLine("---------------------降序排列结果如下:---------------------");
#region 降序方法1
//降序方法一:
Dictionary<string, string> desDic = dic.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value.ToString());
foreach (KeyValuePair<string, string> kv in desDic)
{
Console.WriteLine("第一种降序方式运行结果key为:" + kv.Key + ", value为" + kv.Value);
}
#endregion
#region 降序方法三
//降序方法二:
Dictionary<string, string> des2Dic = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> kv in sorDic.Reverse())//Reverse()方法为反转元素顺序
{
des2Dic.Add(kv.Key, kv.Value);
Console.WriteLine("将SortedDictionary<string, string>升序以后的数据进行降序的key为:" + kv.Key + ", value为:" + kv.Value);
}
#endregion
#region 降序方法三
//降序方法三:
arrayList.Reverse();
StringBuilder sub = new StringBuilder();
Dictionary<string, string> desrDic = new Dictionary<string, string>();
foreach (string al in arrayList)//注意arrayList与al的数据类型是子集关系
{
sub.Append(al).Append(dic[al]);//得到数据为key1value1key2value2
desrDic.Add(al, dic[al]);
}
foreach (KeyValuePair<string, string> kv in desrDic)
{
Console.WriteLine("方法3的ArrayList逆序以后顺序为key:" + kv.Key + ", value为:" + kv.Value);
}
Console.WriteLine("方法3降序以后的顺序为:" + sub);
#endregion
}
}
operation result: