diff --git a/src/DynamicData/Binding/SortAndBind.cs b/src/DynamicData/Binding/SortAndBind.cs index 6a6887dc..c22ef8bf 100644 --- a/src/DynamicData/Binding/SortAndBind.cs +++ b/src/DynamicData/Binding/SortAndBind.cs @@ -192,8 +192,7 @@ private void ApplyChanges(IChangeSet changes) updatedIndex = currentIndex < updatedIndex ? updatedIndex - 1 : updatedIndex; if (updatedIndex != currentIndex) { - target.RemoveAt(currentIndex); - target.Insert(updatedIndex, item); + target.Move(currentIndex, updatedIndex, item); } } break; diff --git a/src/DynamicData/Cache/Internal/SortExtensions.cs b/src/DynamicData/Cache/Internal/SortExtensions.cs index 89f16cf7..05c2cad0 100644 --- a/src/DynamicData/Cache/Internal/SortExtensions.cs +++ b/src/DynamicData/Cache/Internal/SortExtensions.cs @@ -2,6 +2,8 @@ // Roland Pheasant licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. +using System.Collections.ObjectModel; + namespace DynamicData.Cache.Internal; internal static class SortExtensions @@ -51,4 +53,22 @@ public static int GetInsertPositionLinear(this IList list, TItem t return list.Count; } + + public static void Move(this IList list, int original, int destination, TItem item) + { + // If the list supports the Move method, use it instead of removing and inserting. + if (list is IExtendedList extendedList) + { + extendedList.Move(original, destination); + } + else if (list is ObservableCollection observableList) + { + observableList.Move(original, destination); + } + else + { + list.RemoveAt(original); + list.Insert(destination, item); + } + } }