I sort a list in xaml using a ListCollectionView and an IComparer, 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;
When looking at the source code 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.
See also the discussion on this forum with title (I'm not allowed to post urls): 'Stable sort using List<T>'