Your code unnecessarily sorts the whole collection on each insert (which is likely going to be O(n log n)). It also produces two events for each insert (one for Add
and one for Move
), which also isn't necessary.
What I would do:
- Find the index where the inserted item belongs.
- Insert the item at that index.
A simple implementation for that could look like this:
public static void AddSorted<T>(this IList<T> list, T item, IComparer<T> comparer = null)
{
if (comparer == null)
comparer = Comparer<T>.Default;
int i = 0;
while (i < list.Count && comparer.Compare(list[i], item) < 0)
i++;
list.Insert(i, item);
}
This could be made faster by using binary search to find the index. Though the total complexity is still going to be O(n), even with binary search, because Insert
is O(n) (it has to move all items after the inserted item).
This means that using binary search would help the most if you're often inserting items at or near the end of the collection.
Other notes about your code:
public void UpdateSource(ObservableCollection<SomeType> source, SomeType newItem)
I don't think Update
is the right name here, this is not a general update, something like AddToSource
would be better.
source.OrderBy(_=>_.SomeProperty)
_
as a parameter name of a lambda is commonly used when you don't care about the value of that parameter. If you do care about it (like here), use something like x
, or, even better, a meaningful name.