मुझे समस्या है कि सूची सॉर्ट विधि सॉर्टिंग के साथ कैसे काम करती है। निम्नलिखित तत्व को देखते हुए:सूची <T> क्यों है। विधि विधि बराबर IComparable <T> तत्वों को पुन: व्यवस्थित करता है?
class Element : IComparable<Element>
{
public int Priority { get; set; }
public string Description { get; set; }
public int CompareTo(Element other)
{
return Priority.CompareTo(other.Priority);
}
}
अगर मैं इसे इस तरह सुलझाने की कोशिश:
List<Element> elements = new List<Element>()
{
new Element()
{
Priority = 1,
Description = "First"
},
new Element()
{
Priority = 1,
Description = "Second"
},
new Element()
{
Priority = 2,
Description = "Third"
}
};
elements.Sort();
तो पहला तत्व पहले से दूसरा तत्व "दूसरा" है। या, दूसरे शब्दों में, इस दावे विफल रहता है:
Assert.AreEqual("First", elements[0].Description);
क्यों नेट मेरी सूची को पुन: क्रम है जब तत्व मूलतः एक ही हैं? अगर मैं तुलना गैर-शून्य मान देता हूं तो मैं केवल सूची को पुन: व्यवस्थित करना चाहता हूं।
एक स्थिर सॉर्ट विधि के लिए फ़ीचर अनुरोध https://github.com/dotnet/corefx/issues/4696 –