Quantcast
Channel: Why does List.Sort method reorder equal IComparable elements? - Stack Overflow
Browsing latest articles
Browse All 8 View Live

Answer by supercat for Why does List.Sort method reorder equal IComparable...

In some applications, when a list of items is sorted according to some criterion, preserving the original order of items which compare equal is unnecessary. In other applications, it is necessary. Sort...

View Article



Answer by xhafan for Why does List.Sort method reorder equal IComparable...

Here is an extension method SortStable() for List<T> where T : IComparable<T>: public static void SortStable<T>(this List<T> list) where T : IComparable<T> { var...

View Article

Answer by Gavin Miller for Why does List.Sort method reorder equal...

If you wanted to sort based on two fields instead of one you could do this: class Element : IComparable<Element> { public int Priority { get; set; } public string Description { get; set; } public...

View Article

Answer by Levi for Why does List.Sort method reorder equal IComparable elements?

See the other responses for why List.Sort() is unstable. If you need a stable sort and are using .NET 3.5, try Enumerable.OrderBy() (LINQ).

View Article

Answer by Scott Wisniewski for Why does List.Sort method reorder equal...

You can fix this by adding an "index value" to your structure, and including that in the CompareTo method when Priority.CompareTo returns 0. You would then need to initialize the "index" value before...

View Article


Answer by Paul Sonier for Why does List.Sort method reorder equal IComparable...

From the documentation of the List.Sort() method from MSDN: This method uses Array.Sort, which uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are...

View Article

Answer by JP Alioto for Why does List.Sort method reorder equal IComparable...

You told it how to compare things and it did. You should not rely on internal implementation of Sort in your application. That's why it let's you override CompareTo. If you want to have a secondary...

View Article

Why does List.Sort method reorder equal IComparable elements?

I have a problem with how the List Sort method deals with sorting. Given the following element: class Element : IComparable<Element> { public int Priority { get; set; } public string Description...

View Article

Browsing latest articles
Browse All 8 View Live




Latest Images