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
上一篇: Java判断List中有无重复元素的方法