using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 字典排序
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> dic = new Dictionary<string, int>
{
{"张飞",60},{"关羽",70},{"刘备",50},{"赵云",80},{"马超",40}
};
Console.WriteLine("---------------排序之前--------------");
foreach (var item in dic)
{
Console.WriteLine("姓名:{0}, 攻击力:{1}", item.Key, item.Value);
}
//按照字典的值进行降序排列.
var result = from d in dic orderby d.Value descending select d;
Console.WriteLine("---------------排序之后--------------");
foreach (var item in result)
{
Console.WriteLine("姓名:{0}, 攻击力:{1}", item.Key, item.Value);
}
Console.ReadKey();
}
}
} |