C# 字典Dictionary<TKey,TValue>排序整理

准备

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

参考:

Enumerable.ToDictionary 方法

SortedDictionary<TKey,TValue> 类

关于C#:根据键对字典进行排序

C#中Dictionary<TKey,TValue>排序方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fightsyj

您的鼓励将是我分享的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值