कस्टम ऑब्जेक्ट्स के साथ c # काम के लिए सॉर्ट किए गए सेट कैसे होते हैं? [डुप्लिकेट]

Dec 14 2020

मैं एक कस्टम ऑब्जेक्ट्स के लिए c # में सॉर्ट किए गए सेटों के साथ खेलने की कोशिश कर रहा हूं और किसी कारण से, ऐसा लगता है कि सॉर्ट किए गए सेट डेटा को स्टोर करने के लिए ऑब्जेक्ट्स के संदर्भ का उपयोग नहीं कर सकते हैं।

निम्नलिखित कोड स्निपेट में, मैं कस्टम वर्ग की गणना की संपत्ति पर भरोसा करने के लिए एक कस्टम IComparer का उपयोग करता हूं। लेकिन किसी कारण से, यह ऐड कार्यक्षमता को प्रभावित करता है। और काउंटर.एडीडी (दो) लाइन सेट के अलावा कोई फर्क नहीं पड़ता भले ही यह एक अलग संदर्भ हो और दो गुणों के लिए एक अलग मूल्य हो।

क्या मैं कुछ भूल रहा हूँ? क्या मुझे कुछ गलत मिला है कि 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);
            }
        }
    }

एक नज़र लेने और मदद करने के लिए धन्यवाद!

जवाब

1 DmitryBychenko Dec 14 2020 at 13:18

अच्छी तरह से केवल अलग आइटम [Sorted]Setशामिल कर सकते हैं ; यानी दो और समान आइटम नहीं हो सकते। आप आइटम की तुलना करते हैं (उन्हें समान मानते हैं) : यदि दो वस्तुओं के समान हैं तो उन्हें समान माना जाता है। अपने कोड मेंSetCountCount

  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यही कारण है कि oneऔर सॉर्ट किए गए सेट के लिए समानtwo हैं । जब आप आइटम जोड़ते हैं, तो दूसरा (जो है ) अनदेखा किया जाता है:countertwo

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

यदि आप अलग मानदंड चाहते हैं (एक के लिए Equalsऔर दूसरा क्रम के लिए है) तो आप अच्छे पुराने की कोशिश 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);