diff --git a/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableSortedCollection_INotifyCollectionChangedTests.cs b/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableSortedCollection_INotifyCollectionChangedTests.cs index bdb617d..6d9a0e6 100644 --- a/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableSortedCollection_INotifyCollectionChangedTests.cs +++ b/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableSortedCollection_INotifyCollectionChangedTests.cs @@ -76,7 +76,8 @@ public void Test_ConcurrentObservableSortedCollection_InsertRange() Assert.AreEqual(returnedObject, collection); Assert.AreEqual(NotifyCollectionChangedAction.Add, returnedArgs.Action); - Assert.AreEqual(startIndex, returnedArgs.NewStartingIndex); + // Sorted collection doesn't add at the index, so it should be -1 + Assert.AreEqual(-1, returnedArgs.NewStartingIndex); Assert.IsNotNull(returnedArgs.NewItems); Assert.IsNull(returnedArgs.OldItems); Assert.AreEqual(toAdd.Count(), returnedArgs.NewItems.Count); diff --git a/Swordfish.NET.CollectionsV3/ConcurrentObservableSortedCollection.cs b/Swordfish.NET.CollectionsV3/ConcurrentObservableSortedCollection.cs index 1115087..d3778ff 100644 --- a/Swordfish.NET.CollectionsV3/ConcurrentObservableSortedCollection.cs +++ b/Swordfish.NET.CollectionsV3/ConcurrentObservableSortedCollection.cs @@ -50,10 +50,12 @@ public override void AddRange(IList items) public override void Reset(IList items) => DoReadWriteNotify( - () => new OldAndNew(ImmutableList.ToArray(),items.OrderBy(x => x, _sorter).ToList()), - (oldAndNew) => ImmutableList.Empty.AddRange(oldAndNew.New), - (oldAndNew) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)oldAndNew.Old, 0), - (oldAndNew) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)oldAndNew.New, 0) + // Sort the incoming collection and add it directly to the internal collection. + // Should be quicker than sorting 1 by 1 on insert. + () => ImmutableList.ToArray(), + (oldItems) => ImmutableList.Empty.AddRange(items.OrderBy(x => x, _sorter).ToList()), + (oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)oldItems, 0), + (oldItems) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, 0) ); public override void Insert(int index, T item) @@ -75,16 +77,6 @@ public override T this[int index] Add(value); } } - - private class OldAndNew { - public OldAndNew(T[] old, List @new) - { - Old = old; - New = @new; - } - public T[] Old; - public List New; - } } }