C# 键值对数据排序

对于键值对的数据进行排序方法总结。

/*使用排序字典,默认只支持升序
SortedDictionary<DateTime, String> dd = new SortedDictionary<DateTime, String>();
DateTime dt = DateTime.Now;
dd.Add(dt, "bbb");
dd.Add(dt.AddDays(-1), "ccc");
dd.Add(dt.AddDays(1), "aaa");
//可以借助List得到降序键或值
List<String> lst = new List<String>(dd.Values);
lst.Reverse();
*/

/*使用Linq查询
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");

var result = from pair in dd orderby pair.Key descending select pair;
List<String> lst = new List<String>();
foreach (var kv in result)
{
    lst.Add(kv.Value);
}
//或
Dictionary<DateTime, String> dd2 = result.ToDictionary(p=>p.Key, p=>p.Value);
*/

//使用扩展方法
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");

Dictionary<DateTime, String> dicAsc = dd.OrderBy(p=>p.Key).ToDictionary(p=>p.Key, p=>p.Value);
Dictionary<DateTime, String> dicDesc = dd.OrderByDescending(p=>p.Key).ToDictionary(p=>p.Key, p=>p.Value);


C#中,支持键值对(key-value pair)的数据结构有多种,每种适用于不同的使用场景。以下是一些常见的键值对数据结构及其特点: ### 1. **Dictionary<TKey, TValue>** `Dictionary<TKey, TValue>` 是C#中最常用的键值对集合类。它存储键值对,并且键必须是唯一的。查找、插入和删除操作的时间复杂度为 O(1),因为它是基于哈希表实现的。该数据结构是类型安全的,键和值都有明确的类型[^1]。 ```csharp Dictionary<string, int> ages = new Dictionary<string, int>(); ages.Add("Alice", 30); ages["Bob"] = 25; ``` ### 2. **Hashtable** `Hashtable` 是一个非泛型的集合类,也可以存储键值对。与 `Dictionary<TKey, TValue>` 不同的是,`Hashtable` 的键和值都是 `object` 类型,因此不具有类型安全性。此外,`Hashtable` 是线程安全的,适合在多线程环境中使用。 ```csharp Hashtable table = new Hashtable(); table["Alice"] = 30; table["Bob"] = 25; ``` ### 3. **SortedDictionary<TKey, TValue>** `SortedDictionary<TKey, TValue>` 是一个基于红黑树实现的键值对集合,键值对按照键的顺序进行排序。它的查找、插入和删除操作的时间复杂度为 O(log n),适用于需要按键排序的场景。 ```csharp SortedDictionary<string, int> sortedAges = new SortedDictionary<string, int>(); sortedAges.Add("Alice", 30); sortedAges["Bob"] = 25; ``` ### 4. **ConcurrentDictionary<TKey, TValue>** `ConcurrentDictionary<TKey, TValue>` 是线程安全的字典类,适用于多线程环境。它提供了原子操作,确保在并发访问时的数据一致性。此数据结构常用于并行编程或需要高并发性能的场景。 ```csharp ConcurrentDictionary<string, int> concurrentAges = new ConcurrentDictionary<string, int>(); concurrentAges.TryAdd("Alice", 30); concurrentAges["Bob"] = 25; ``` ### 5. **ImmutableDictionary<TKey, TValue>** `ImmutableDictionary<TKey, TValue>` 是不可变的字典类,适用于需要保持数据不变性的场景。每次修改操作都会返回一个新的字典实例,而原始字典保持不变。这种数据结构在函数式编程或需要避免副作用的场景中非常有用。 ```csharp ImmutableDictionary<string, int> immutableAges = ImmutableDictionary<string, int>.Empty; immutableAges = immutableAges.Add("Alice", 30); ``` ### 6. **NameValueCollection** `NameValueCollection` 是一个特殊的键值对集合,允许一个键对应多个值。它通常用于处理HTTP查询字符串或表单数据。 ```csharp NameValueCollection collection = new NameValueCollection(); collection.Add("Colors", "Red"); collection.Add("Colors", "Blue"); ``` ### 7. **Lookup<TKey, TElement>** `Lookup<TKey, TElement>` 是一个只读的键值对集合,其中每个键可以对应多个值。它通常用于从一个集合中根据某个键分组数据。 ```csharp var lookup = new List<string> { "apple", "banana", "apricot" } .ToLookup(s => s[0]); foreach (var group in lookup) { Console.WriteLine($"Key: {group.Key}"); foreach (var item in group) { Console.WriteLine(item); } } ``` ### 8. **Tuple 作为键值对** C# 中的 `ValueTuple` 可以用于表示键值对。虽然它不是集合类,但可以作为单个键值对的表示形式,尤其在返回多个值的场景中非常有用。 ```csharp var pair = (Key: "Alice", Value: 30); Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}"); ``` ### 9. **自定义键值对类** 如果需要更复杂的键值对逻辑,可以定义自己的类来表示键值对。这种方式提供了最大的灵活性,但需要手动实现相关的功能。 ```csharp public class CustomKeyValuePair<TKey, TValue> { public TKey Key { get; set; } public TValue Value { get; set; } public CustomKeyValuePair(TKey key, TValue value) { Key = key; Value = value; } } ``` ### 10. **InfluxDB 的字段(Fields)** 在 InfluxDB 中,字段(Fields)是键值对的一部分,用于存储实际的数据值。字段是必需的,但它们不会被索引,因此基于字段的查询性能不如基于标签(Tags)的查询[^2]。 ### 11. **使用 `using` 定义语义别名** C# 允许通过 `using` 指令为复杂的类型(如元组)定义语义别名,从而提高代码的可读性和可维护性。例如,可以为 `ValueTuple<int, int>` 定义一个别名 `Coordinate`[^4]。 ```csharp using Coordinate = System.ValueTuple<int, int>; Coordinate location = (10, 20); ``` ###
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值