Fix post-sort index tracking in RBTree insertion sort #11258
+80
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using insertion sort to restore ListCollectionView sort order when live sorting is in effect, ensure that the new index (after sort) is resolved as a global index within the entire tree rather than a local index within a RBNode subtree.
Fixes # 11257
Description
When using a ListCollectionView with live sorting enabled, occasionally the CollectionChanged event will emit a NotifyCollectionChangedAction.Move notification where the NotifyCollectionChangedEventArgs.NewStartingIndex property doesn't match the actual index of the item in the collection found with ListCollectionView.IndexOf(object item).
This change addresses the bug responsible for the observed incorrect behavior, fixing a code path where a local subtree index would be provided as the NewStartingIndex instead of the global index within the live shaping tree.
Customer Impact
If the collection view is used as an items source for a WPF component such as ListBox, this can result in incorrect sorting behavior or duplicate items appearing.
Regression
I believe this bug was carried over from the .NET Framework implementation.
Testing
In addition to the unit test ensuring the
NotifyCollectionChangedEventArgsis consistent with both the source and observed collection, this has been tested in a real WPF application to confirm that this addresses the incorrect UI behavior experienced before.Risk
I believe this to be low risk. There are no structural changes to the code. There is no performance cost beyond simple integer arithmetic. Both the incorrect behavior and fix are easily understandable to those who are familiar with the domain. The change is covered with a new unit test validating behavior.
Microsoft Reviewers: Open in CodeFlow