Skip to content

Conversation

@nalywa
Copy link

@nalywa nalywa commented Nov 16, 2025

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 NotifyCollectionChangedEventArgs is 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

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.

Fix dotnet#11257
@nalywa nalywa requested a review from a team as a code owner November 16, 2025 15:52
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Nov 16, 2025
@nalywa
Copy link
Author

nalywa commented Nov 16, 2025

@dotnet-policy-service agree

Copy link
Member

@lindexi lindexi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@h3xds1nz h3xds1nz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ListCollectionView generating bad NewStartingIndex on LiveSorting

4 participants