Tôi gặp sự cố với cách phương thức sắp xếp danh sách xử lý sắp xếp. Với các yếu tố sau:Tại sao Danh sách <T>. Sắp xếp lại phương thức sắp xếp bằng IComparable <T> yếu 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);
}
}
Nếu tôi cố gắng sắp xếp nó theo cách này:
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();
Sau đó, các yếu tố đầu tiên là yếu tố trước đây thứ hai "Thứ hai". Hoặc nói cách khác, xác nhận này không thành công:
Assert.AreEqual("First", elements[0].Description);
Tại sao .NET sắp xếp lại danh sách của tôi khi các yếu tố cơ bản giống nhau? Tôi muốn cho nó để chỉ sắp xếp lại danh sách nếu so sánh trả về một giá trị khác không.
Yêu cầu tính năng cho phương thức sắp xếp ổn định https://github.com/dotnet/corefx/issues/4696 –