.NET SortedDictionary但按值排序 [英] .NET SortedDictionary But Sorted By Values

查看:268
本文介绍了.NET SortedDictionary但按值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个类似 SortedDictionary< int,double> 的数据结构,但是基于值而不是键进行排序。我需要大约1-2微秒来添加和删除项目,当我们在字典中有约3000个项目。

I need a data structure that acts like a SortedDictionary<int, double> but is sorted based on the values rather than the keys. I need it to take about 1-2 microseconds to add and remove items when we have about 3000 items in the dictionary.

我的第一个想法是简单地切换键和值在我的代码。这非常接近工作。我可以在我的测试中通过这样做在大约1.2微秒添加和删除元素。

My first thought was simply to switch the keys and values in my code. This very nearly works. I can add and remove elements in about 1.2 microseconds in my testing by doing this.

但是键在SortedDictionary中必须是唯一的,这意味着在我的逆字典必须是唯一的。

But the keys have to be unique in a SortedDictionary so that means that values in my inverse dictionary would have to be unique. And there are some cases where they may not be.

在.NET库中已经有对我有用的任何想法?

Any ideas of something in the .NET libraries already that would work for me?

推荐答案

PowerCollections 库具有类 OrderedMultiDictionary 基本上类似于 SortedDictionary< TKey,TValue> ,但允许重复。当你查找一个键,你得到一个枚举而不是一个单一的值。

The PowerCollections library has a class called OrderedMultiDictionary<TKey, TValue> that is basically like a SortedDictionary<TKey, TValue> but allows duplicates. When you lookup a key, you get an enumerable instead of a single value.

库是免费的,你应该能够做到你想要的类 - 将值存储为键。

The library is free and you should be able to do exactly what you want with that class - store the values as the keys.

这篇关于.NET SortedDictionary但按值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆