Skip to content

Commit

Permalink
SortAndBind uses Move instead of RemoveAt/Insert when applicable. (#936)
Browse files Browse the repository at this point in the history
Co-authored-by: Kristian Pettersen <[email protected]>
  • Loading branch information
kristian-pettersen and kristian-pettersen authored Sep 4, 2024
1 parent b6e851e commit 55002ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/DynamicData/Binding/SortAndBind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ private void ApplyChanges(IChangeSet<TObject, TKey> changes)
updatedIndex = currentIndex < updatedIndex ? updatedIndex - 1 : updatedIndex;
if (updatedIndex != currentIndex)
{
target.RemoveAt(currentIndex);
target.Insert(updatedIndex, item);
target.Move(currentIndex, updatedIndex, item);
}
}
break;
Expand Down
20 changes: 20 additions & 0 deletions src/DynamicData/Cache/Internal/SortExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,4 +53,22 @@ public static int GetInsertPositionLinear<TItem>(this IList<TItem> list, TItem t

return list.Count;
}

public static void Move<TItem>(this IList<TItem> 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<TItem> extendedList)
{
extendedList.Move(original, destination);
}
else if (list is ObservableCollection<TItem> observableList)
{
observableList.Move(original, destination);
}
else
{
list.RemoveAt(original);
list.Insert(destination, item);
}
}
}

0 comments on commit 55002ae

Please sign in to comment.