Skip to content

Commit 55002ae

Browse files
authoredSep 4, 2024··
SortAndBind uses Move instead of RemoveAt/Insert when applicable. (#936)
Co-authored-by: Kristian Pettersen <kristian.pettersen@knc.kongsberg.com>
1 parent b6e851e commit 55002ae

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
 

‎src/DynamicData/Binding/SortAndBind.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ private void ApplyChanges(IChangeSet<TObject, TKey> changes)
192192
updatedIndex = currentIndex < updatedIndex ? updatedIndex - 1 : updatedIndex;
193193
if (updatedIndex != currentIndex)
194194
{
195-
target.RemoveAt(currentIndex);
196-
target.Insert(updatedIndex, item);
195+
target.Move(currentIndex, updatedIndex, item);
197196
}
198197
}
199198
break;

‎src/DynamicData/Cache/Internal/SortExtensions.cs

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Roland Pheasant licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for full license information.
44

5+
using System.Collections.ObjectModel;
6+
57
namespace DynamicData.Cache.Internal;
68

79
internal static class SortExtensions
@@ -51,4 +53,22 @@ public static int GetInsertPositionLinear<TItem>(this IList<TItem> list, TItem t
5153

5254
return list.Count;
5355
}
56+
57+
public static void Move<TItem>(this IList<TItem> list, int original, int destination, TItem item)
58+
{
59+
// If the list supports the Move method, use it instead of removing and inserting.
60+
if (list is IExtendedList<TItem> extendedList)
61+
{
62+
extendedList.Move(original, destination);
63+
}
64+
else if (list is ObservableCollection<TItem> observableList)
65+
{
66+
observableList.Move(original, destination);
67+
}
68+
else
69+
{
70+
list.RemoveAt(original);
71+
list.Insert(destination, item);
72+
}
73+
}
5474
}

0 commit comments

Comments
 (0)
Please sign in to comment.