准备
1、Student 类
public class Student
{
public string name {get; set;}
public int age {get; set;}
public int score {get; set;}
}
2、dumpDictionary 方法
void dumpDictionary(Dictionary<int, Student> stuDict, string desc)
{
Console.WriteLine(desc);
foreach (KeyValuePair<int, Student> kvp in stuDict)
{
Console.WriteLine("{0} : {1} {2} {3}", kvp.Key, kvp.Value.name, kvp.Value.age, kvp.Value.score);
}
}
3、原始字典
Dictionary<int, Student> stuDict = new Dictionary<int, Student> {
[4] = new Student() {name = "张三", age = 20, score = 95},
[5] = new Student() {name = "李四", age = 19, score = 99},
[1] = new Student() {name = "王五", age = 21, score = 95},
[3] = new Student() {name = "赵六", age = 20, score = 90},
[6] = new Student() {name = "陈七", age = 22, score = 95},
[2] = new Student() {name = "刘八", age = 21, score = 92}
};
dumpDictionary(stuDict, "-----原始字典-----");
// -----原始字典-----
// 4 : 张三 20 95
// 5 : 李四 19 99
// 1 : 王五 21 95
// 3 : 赵六 20 90
// 6 : 陈七 22 95
// 2 : 刘八 21 92
按键(Key)排序
OrderBy/OrderByDescending
// 升序
stuDict = stuDict.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Key 升序-----");
// -----按 Key 升序-----
// 1 : 王五 21 95
// 2 : 刘八 21 92
// 3 : 赵六 20 90
// 4 : 张三 20 95
// 5 : 李四 19 99
// 6 : 陈七 22 95
// 降序
stuDict = stuDict.OrderByDescending(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Key 降序-----");
// -----按 Key 降序-----
// 6 : 陈七 22 95
// 5 : 李四 19 99
// 4 : 张三 20 95
// 3 : 赵六 20 90
// 2 : 刘八 21 92
// 1 : 王五 21 95
SortedDictionary
SortedDictionary<int, Student> sortStuDict = new SortedDictionary<int, Student> (stuDict);
Console.WriteLine("-----SortedDictionary-----");
foreach (KeyValuePair<int, Student> kvp in sortStuDict)
{
Console.WriteLine("{0} : {1} {2} {3}", kvp.Key, kvp.Value.name, kvp.Value.age, kvp.Value.score);
}
// -----SortedDictionary-----
// 1 : 王五 21 95
// 2 : 刘八 21 92
// 3 : 赵六 20 90
// 4 : 张三 20 95
// 5 : 李四 19 99
// 6 : 陈七 22 95
ps
SortedDictionary 默认升序。
按值(Value)排序
OrderBy/OrderByDescending
单属性排序
// 升序
stuDict = stuDict.OrderBy(kv => kv.Value.age).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Value 的 age 属性升序-----");
// -----按 Value 的 age 属性升序-----
// 5 : 李四 19 99
// 4 : 张三 20 95
// 3 : 赵六 20 90
// 1 : 王五 21 95
// 2 : 刘八 21 92
// 6 : 陈七 22 95
// 降序
stuDict = stuDict.OrderByDescending(kv => kv.Value.score).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Value 的 score 属性降序-----");
// -----按 Value 的 score 属性降序-----
// 5 : 李四 19 99
// 4 : 张三 20 95
// 1 : 王五 21 95
// 6 : 陈七 22 95
// 2 : 刘八 21 92
// 3 : 赵六 20 90
嵌套排序
// 按 Value 的 score 属性降序,score 相同则按 Value 的 age 升序
stuDict = stuDict.OrderByDescending(kv => kv.Value.score).ThenBy(kv => kv.Value.age).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Value 的 score 属性降序,score 相同则按 Value 的 age 升序-----");
// -----按 Value 的 score 属性降序,score 相同则按 Value 的 age 升序-----
// 5 : 李四 19 99
// 4 : 张三 20 95
// 1 : 王五 21 95
// 6 : 陈七 22 95
// 2 : 刘八 21 92
// 3 : 赵六 20 90
// 按 Value 的 age 属性升序,age 相同则按 Key 降序
stuDict = stuDict.OrderBy(kv => kv.Value.age).ThenByDescending(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Value 的 age 属性升序,age 相同则按 Key 降序-----");
// -----按 Value 的 age 属性升序,age 相同则按 Key 降序-----
// 5 : 李四 19 99
// 4 : 张三 20 95
// 3 : 赵六 20 90
// 2 : 刘八 21 92
// 1 : 王五 21 95
// 6 : 陈七 22 95
LINQ语句
// 升序
stuDict = (from pair in stuDict orderby pair.Value.age select pair).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Value 的 age 属性升序-----");
// -----按 Value 的 age 属性升序-----
// 5 : 李四 19 99
// 4 : 张三 20 95
// 3 : 赵六 20 90
// 1 : 王五 21 95
// 2 : 刘八 21 92
// 6 : 陈七 22 95
// 降序
stuDict = (from pair in stuDict orderby pair.Value.score descending select pair).ToDictionary(kv => kv.Key, kv => kv.Value);
dumpDictionary(stuDict, "-----按 Value 的 score 属性降序-----");
// -----按 Value 的 score 属性降序-----
// 5 : 李四 19 99
// 4 : 张三 20 95
// 1 : 王五 21 95
// 6 : 陈七 22 95
// 2 : 刘八 21 92
// 3 : 赵六 20 90
参考: