3

I sort a list in xaml using a ListCollectionView and a Comparer, and in another location in C# I need to sort in the exact same way, using the same Comparer, so the sort needs to be stable.

This sort is unstable:

myCollectionCopy = myCollection.ToList();
myCollectionCopy.Sort(myComparer);

This sort is stable:

myCollection.OrderBy(x => x.MyProperty, myComparer)

But what I can't figure out from the documentation is if ListCollectionView uses a stable sort when setting

myListCollectionView.CustomSort = myComparer;

See also: https://social.msdn.microsoft.com/Forums/vstudio/en-US/f5ea4976-1c3d-4e10-90e7-c7a0491fc28a/stable-sort-using-listt?forum=netfxbcl

eriksmith200
  • 2,159
  • 5
  • 22
  • 33
  • Isn't it safer to implement a stable sort yourself? Or including the index as a parameter in your comparer? What type are your list items? – oriash Sep 20 '18 at 09:35
  • The list items are of type 'MyObject' :p (so no simple type). When implementing a stable sort that can be used in the collectionview then I probably need to modify the Comparer so that it knows the index of the items, but how do I do that in a way that the CollectionView can use this? – eriksmith200 Sep 20 '18 at 09:37
  • I think these sort is unstable, because customSort just implement the IComparer of object, but probably still calls the sort function later on. You can add guide number, and compare the guide when two object will be the same, then you will have stable. – Kaspar Sep 20 '18 at 09:41
  • Either implement a stable sort of your own (based on merge sort, insertion sort, etc), or a non-stable sort including the original index when comparing (assuming your object has some index property). – oriash Sep 20 '18 at 09:45
  • @Kaspar it is possible to do both a stable and an unstable sort with an IComparer. If CollectionView does a stable sort (using the comparer) then I don't need to modify the comparer. – eriksmith200 Sep 20 '18 at 10:28
  • @oriash but is it necessary? (and the object does not have an index property) – eriksmith200 Sep 20 '18 at 10:30
  • Not necessary, but that's what I would do. – oriash Sep 20 '18 at 10:36
  • When looking at the source of ListCollectionView, it seems that both the return value of ICompare.Compare and also the index of the (source? listview?)collection is used (the comparer seems to be stored in the ActiveComparer property). So it could be a stable sort but I'm not sure just by looking at this code. – eriksmith200 Sep 20 '18 at 10:49
  • @oriash not me if I'm not sure that it is necessary, and especially since the objects being sorted don't have an index property. Writing potentially unnecessary code for this issue could be a great source of bugs or performance issues. – eriksmith200 Sep 20 '18 at 10:51
  • This experience by the topic starter seems to suggest that ListCollectionView does not use a stable sort: https://social.msdn.microsoft.com/Forums/vstudio/en-US/15997d82-7ee8-4674-b8cb-09454320620e/question-about-the-customsort – eriksmith200 Sep 27 '18 at 06:55

0 Answers0