[英]SortedDictionary with composite key: indexed by 1 property and sorted by another
I am trying to implement a SortedDictionary as: 我正在尝试将SortedDictionary实现为:
public IDictionary<CustomAttributeDataKey, object> Data { get; } = new SortedDictionary<CustomAttributeDataKey, object>();
I have defined CustomAttributeDataKey as: 我将CustomAttributeDataKey定义为:
public class CustomAttributeDataKey : IComparable {
public long CustomAttributeID { get; set; }
public int Order { get; set; }
//ASSUMING this is used to check if there is a collision of keys
protected bool Equals(CustomAttributeDataKey other) {
return CustomAttributeID == other.CustomAttributeID;
}
//ASSUMING this is used to check if there is a collision of keys
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((CustomAttributeDataKey) obj);
}
//ASSUMING this is used to check if the key exists in the dictionary
public override int GetHashCode() {
return CustomAttributeID.GetHashCode();
}
//ASSUMING this is used for sorting
public int CompareTo(object y) {
if (y == null)
return 0;
return this.Order.CompareTo(((CustomAttributeDataKey) y).Order);
}
}
Here's what I would like to happen: 这是我想发生的事情:
Whenever I add items to the dictionary I don't see any breakpoints in the "GetHashCode" or "Equals" methods being hit but I am seeing a breakpoint being hit in "CompareTo". 每当我将项目添加到字典中时,在“ GetHashCode”或“ Equals”方法中都看不到任何断点,但在“ CompareTo”中却遇到了断点。 So not sure how SortedDictionary would actually work in this case scenario.
因此,不确定在这种情况下SortedDictionary实际如何工作。 I'd be interested to find out how to make SortedDictionary work in this scenario (I know I can do this through Linq's OrderBy() method).
我很想知道在这种情况下如何使SortedDictionary工作(我知道我可以通过Linq的OrderBy()方法来做到这一点)。
Here's what I would like to happen:
这是我想发生的事情:
I would like to key the dictionary on "CustomAttributeID" property ONLY
我只想在“ CustomAttributeID”属性上键入字典
I would like the dictionary to sort by "Order" property when I enumerate the keys.
枚举键时,我希望字典按“ Order”属性排序。
Whenever I add items to the dictionary I don't see any breakpoints in the "GetHashCode" or "Equals" methods being hit but I am seeing a breakpoint being hit in "CompareTo".
每当我将项目添加到字典中时,在“ GetHashCode”或“ Equals”方法中都看不到任何断点,但在“ CompareTo”中却遇到了断点。 So not sure how SortedDictionary would actually work in this case scenario.
因此,不确定在这种情况下SortedDictionary实际如何工作。 I'd be interested to find out how to make SortedDictionary work in this scenario (I know I can do this through Linq's OrderBy() method).
我很想知道在这种情况下如何使SortedDictionary工作(我知道我可以通过Linq的OrderBy()方法来做到这一点)。
Per The MSDN , SortedDictionary
is stored using a binary search tree (Per The MSDN , SortedList
is stored using a sorted list of Key-Value pairs) . 每个MSDN的
SortedDictionary
使用二进制搜索树存储(每个MSDN的 SortedList
使用键值对的排序列表存储)。 This has a few implications which may be relevant to you: 这可能与您有关:
SortedDictionary
uses an IComparer
, not GetHashCode
/ Equals
. SortedDictionary
使用IComparer
,而不是GetHashCode
/ Equals
。 Equals
is insufficient to gain a total ordering and similultaneously using Equals
and CompareTo
would be a bad idea, so Equals
is not used at all. Equals
不足以获取总订单,并且同时使用Equals
和CompareTo
将是一个坏主意,因此根本不使用Equals
。 SortedDictionary
are both O(log(n)). SortedDictionary
都是O(log(n))。 SortedDictionary
retains the order of the dictionary. SortedDictionary
保留字典的顺序。 The way to control the order of the enumeration of the SortedDictionary
is by ensuring that CompareTo
orders the dctionary in the same way. SortedDictionary
的枚举SortedDictionary
是通过确保CompareTo
以相同的方式对字典进行排序。 Personally, I would hesitate to order a SortedDictionary
by an "Order" property, as sorting by such a property means you can only lookup members by their position. 就我个人而言,我会犹豫是否要通过“ Order”属性对
SortedDictionary
进行排序,因为按这种属性进行排序意味着您只能按成员的位置查找成员。 This provides a strong link between object position and object equality which does not normally exist. 这在对象位置和对象相等性之间提供了牢固的联系,而这通常是不存在的。 At that point, you may as well use a sorted
List<T>
instead. 在这一点上,您最好也使用排序后的
List<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.