【问题标题】:SortedDictionary with composite key: indexed by 1 property and sorted by another具有复合键的 SortedDictionary:由 1 个属性索引并由另一个属性排序
【发布时间】:2018-09-27 21:26:16
【问题描述】:

我正在尝试将 SortedDictionary 实现为:

public IDictionary<CustomAttributeDataKey, object> Data { get; } = new SortedDictionary<CustomAttributeDataKey, object>();

我已将 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);
    }
}

这是我希望发生的事情:

  1. 我只想在“CustomAttributeID”属性上键入字典
  2. 我希望字典在枚举键时按“Order”属性排序。

每当我向字典中添加项目时,我都看不到“GetHashCode”或“Equals”方法中的任何断点被击中,但我看到“CompareTo”中的断点被击中。所以不确定 SortedDictionary 在这种情况下如何实际工作。我很想知道如何使 SortedDictionary 在这种情况下工作(我知道我可以通过 Linq 的 OrderBy() 方法做到这一点)。

【问题讨论】:

  • 你没有使用 Y.order。
  • 是的,顺序不应该是字典键的一部分。它只是用于排序。
  • 您没有按订单排序。
  • 这是模棱两可的。如果您有两个 CustomAttributeDataKeys 具有相同的 CustomAttributeID 但具有不同的 Order 值,您期望会发生什么?
  • 如果这是您想要的,那么 SortedDictionary 不适合您,因为它不是这样做的。它的工作是维护一个排序的键列表,每个键都映射到一个值,并且它做得很好。在我看来,您想要的是两件事:一个哈希表,它是从 id 到对象的映射,以及一个排序字典,它是从订单到 id 的映射。当您想通过 id 查找时,请查看哈希表。当您想枚举 id 时,请枚举订单并获取 id。您可以轻松构建这样的类型并使其实现 IDictionary。

标签: c# .net sorteddictionary


【解决方案1】:

这是我希望发生的事情:

  1. 我想在“CustomAttributeID”属性上键入字典 仅限

  2. 我希望字典按“顺序”属性排序,当我 枚举键。

每当我将项目添加到我没有看到的字典时 “GetHashCode”或“Equals”方法中的任何断点被命中,但 我在“CompareTo”中看到了一个断点。所以不确定如何 SortedDictionary 在这种情况下实际上可以工作。我会 有兴趣了解如何使 SortedDictionary 在此工作 场景(我知道我可以通过 Linq 的 OrderBy() 方法做到这一点)。

The MSDNSortedDictionary 使用二叉搜索树存储(每The MSDNSortedList 使用键值对的排序列表存储)。这有一些可能与您相关的含义:

  1. SortedDictionary 使用 IComparer,而不是 GetHashCode/EqualsEquals 不足以获得总排序,同时使用 EqualsCompareTo 将是一个坏主意,因此根本不使用 Equals
  2. 检索和插入SortedDictionary 都是 O(log(n))。
  3. 根据The MSDN,枚举SortedDictionary 会保留字典的顺序。控制SortedDictionary 的枚举顺序的方法是确保CompareTo 以相同的方式对字典进行排序。

就个人而言,我会犹豫是否通过“Order”属性订购SortedDictionary,因为按此类属性排序意味着您只能按位置查找成员。这提供了对象位置和对象相等性之间的强联系,这通常不存在。此时,您不妨改用已排序的List&lt;T&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多