C#集合Dictionary中按值的排序
static void Main(string[] args)
{
Dictionary<string, int> dt = new
Dictionary<string, int>();
dt.Add("美丽",2);
dt.Add("校园",1);
dt.Add("大学",3);
var result1 = from pair in dt select
pair;
foreach (KeyValuePair<string, int>
pair in result1)
{
Console.WriteLine(pair.Key.ToString() + " " +
pair.Value.ToString());
}
Console.WriteLine();
var result2 = from pair in dt orderby pair.Value select
pair;
foreach (KeyValuePair<string, int>
pair in result2)
{
Console.WriteLine(pair.Key.ToString()+"
"+pair.Value.ToString());
}
Console.WriteLine();
var result3 = from pair in dt orderby pair.Value descending select
pair;
foreach (KeyValuePair<string, int>
pair in result3)
{
Console.WriteLine(pair.Key.ToString() + " " +
pair.Value.ToString());
}
Console.Read();
}
结果输出: