ArrayList.Sort should be a stable sort with an IComparer but is not?
Posted
by
Kaleb Pederson
on Stack Overflow
See other posts from Stack Overflow
or by Kaleb Pederson
Published on 2011-02-18T23:13:59Z
Indexed on
2011/02/18
23:25 UTC
Read the original article
Hit count: 278
A stable sort is a sort that maintains the relative ordering of elements with the same value.
The docs on ArrayList.Sort say that when an IComparer
is provided the sort is stable:
If comparer is set to null, this method performs a comparison sort (also called an unstable sort); that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. To perform a stable sort, you must implement a custom IComparer interface.
Unless I'm missing something, the following testcase shows that ArrayList.Sort
is not using a stable sort:
internal class DisplayOrdered {
public int ID { get; set; }
public int DisplayOrder { get; set; }
public override string ToString() {
return string.Format("ID: {0}, DisplayOrder: {1}", ID, DisplayOrder);
}
}
internal class DisplayOrderedComparer : IComparer {
public int Compare(object x, object y) {
return ((DisplayOrdered)x).DisplayOrder - ((DisplayOrdered)y).DisplayOrder;
}
}
[TestFixture]
public class ArrayListStableSortTest {
[Test]
public void TestWeblinkCallArrayListIsSortedUsingStableSort() {
var call1 = new DisplayOrdered {ID = 1, DisplayOrder = 0};
var call2 = new DisplayOrdered {ID = 2, DisplayOrder = 0};
var call3 = new DisplayOrdered {ID = 3, DisplayOrder = 2};
var list = new ArrayList {call1, call2, call3};
list.Sort(new DisplayOrderedComparer());
// expected order (by ID): 1, 2, 3 (because the DisplayOrder
// is equal for ID's 1 and 2, their ordering should be
// maintained for a stable sort.)
Assert.AreEqual(call1, list[0]); // Actual: ID=2 ** FAILS
Assert.AreEqual(call2, list[1]); // Actual: ID=1
Assert.AreEqual(call3, list[2]); // Actual: ID=3
}
}
Am I missing something? If not, would this be a documentation bug or a library bug?
Apparently using an OrderBy in Linq gives a stable sort.
© Stack Overflow or respective owner