亲宝软件园·资讯

展开

C# 键值对数据排序 C# 键值对数据排序代码

人气:0
想了解C# 键值对数据排序代码的相关内容吗,在本文为您仔细讲解C# 键值对数据排序的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,键值对,数据排序,下面大家一起来学习吧。

复制代码 代码如下:

public static void SortDicWithLinq(){
            Dictionary<DateTime, String> dd = new Dictionary<DateTime, String>();
            DateTime dt = DateTime.Now;
            dd.Add(dt, "bbb");
            dd.Add(dt.AddDays(-1),"ccc");
            dd.Add(dt.AddDays(1), "aaa");

            Console.WriteLine("Org===============");
            travelDicKeyDate(dd);


            Dictionary<DateTime, String> dicAsc = dd.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value);
            Console.WriteLine("Asc===============");
            travelDicKeyDate(dicAsc);


            Dictionary<DateTime, String> dicDesc = dd.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, p => p.Value);
            Console.WriteLine("Desc===============");
            travelDicKeyDate(dicDesc);
        }        public static void travelDicKeyDate(Dictionary<DateTime, String> dic)
        {
            foreach (KeyValuePair<DateTime,String> kvp in dic)
            {
                Console.WriteLine(String.Format("{0}  {1}",kvp.Key,kvp.Value));
            }
        }

 
output:

Org===============
2013/9/27 11:07:29  bbb
2013/9/26 11:07:29  ccc
2013/9/28 11:07:29  aaa
Asc===============
2013/9/26 11:07:29  ccc
2013/9/27 11:07:29  bbb
2013/9/28 11:07:29  aaa
Desc===============
2013/9/28 11:07:29  aaa
2013/9/27 11:07:29  bbb
2013/9/26 11:07:29  ccc

加载全部内容

相关教程
猜你喜欢
用户评论