c# - C# 的排序集如何与自定义对象一起使用?

标签 c# hash sortedset

我正在尝试使用 C# 中的排序集来处理自定义对象,并且出于某种原因,排序集可能没有使用对象的引用来存储数据。

在下面的代码片段中,我使用自定义 IComparer 来依赖自定义类的 Counts 属性。但由于某种原因,这似乎影响了添加功能。并且 counter.Add(two) 行不会对集合进行任何添加,即使它是不同的引用并且两个属性具有不同的值。

我错过了什么吗?我对 SortedSets 在 C# 中的工作方式有什么误解吗?

代码片段

    public class SortedStructureTesting
    {
        public void TestingSortedSets()
        {
            SortedSet<CounterSetup> counter = new SortedSet<CounterSetup>(new CompareCounts());

            CounterSetup one = new CounterSetup(1);
            CounterSetup two = new CounterSetup(2);
            CounterSetup three = new CounterSetup(3, 2);

            counter.Add(one);
            counter.Add(two); // Does not work. This value does not get added to the set.
            counter.Add(three);

            var max = counter.Max;
            counter.Remove(max);
            var sec = counter.Max;
            counter.Remove(sec);
        }

        public class CounterSetup
        {
            public static Random random = new Random();
            public CounterSetup(int no, int cnt = 1)
            {
                Number = no;
                Count = cnt;
                Blah = new string(Guid.NewGuid().ToString());
            }

            public int Number { get; private set; }

            public int Count { get; set; }

            public string Blah { get; private set; }
        }

        public class CompareCounts : IComparer<CounterSetup>
        {
            public int Compare(CounterSetup one, CounterSetup two)
            {
                return one.Count.CompareTo(two.Count);
            }
        }
    }

感谢您的查看和帮助!

最佳答案

好吧,[Sorted]Set 只能包含不同 项;即 Set 不能再有两个相等的项目。您可以根据 Count 来比较项目(将它们视为相等):如果两个项目具有相同的 Count,则它们被视为相等。在您的代码中

  CounterSetup one = new CounterSetup(1);         // one.Count == 1
  CounterSetup two = new CounterSetup(2);         // two.Count == 1
  CounterSetup three = new CounterSetup(3, 2);    // three.Count == 2

你有one.Count == Two.Count == 1,这就是为什么onetwo相等 用于 counter 排序集。添加项目时,第二个(two)将被忽略:

  counter.Add(one);
  counter.Add(two); // Ignored: there's already an item (one) with the same Count
  counter.Add(three);

如果您想要单独条件(一个用于等于,另一个用于顺序),您可以尝试使用旧的HashSet,您可以表示它在 Linq 的帮助下订购:

  using System.Linq;

  ...

  // Items in counter are unique (based on MyEqualityComparer)
  HashSet<CounterSetup> counter = new HashSet<CounterSetup>(
    new MyEqualityComparer()
  );

  // Now we order counter items by different criterium (here we sort by `Count`)
  var ordered = counter
    .OrderBy(item => item.Count);

关于c# - C# 的排序集如何与自定义对象一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65284332/

相关文章:

ruby - 将 ruby​​ 哈希转换为数组的最佳方法是什么

c# - 非常大的集合的效率;迭代和排序

redis - ZREMRANGEBYRANK 与 ZREMRANGEBYSCORE 之间的区别

c# - 有没有办法简化这个开关盒?

c# - 命名空间的奇怪问题

c# - 使用静态工厂是依赖注入(inject)的有效模式吗?

c# - Silverlight new TouchPoint() 导致程序崩溃

android - SHA-256 哈希在 Android 中产生错误的结果

Perl:while ($key = each %hash) 不会在 key = 0 处停止

java - 使用 Comparator 对值进行排序会更改该对象的所有值