Implementation of ApplySort method from IBindingList I am currently developing a family of keyed collections, and I wanted to have one which would implement the IBindingList interface.

My BindingKeyedCollection is derived from a KeyedCollection which owns a look-up dictionary, used to make the researches by key.

However, I have suspicions about my implementation of the ApplySort method (a method used to sort the collection relatively to one of the items property). It seems to be working as it is now, but I am not sure that all items are synchronized properly.

protected virtual void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
    {
        this.SortProperty = property;
        this.SortDirection = direction;
        if (direction == ListSortDirection.Ascending)
        {
            ResetItems(this.OrderBy(item =&gt; property.GetValue(item)).ToList());
            IsSorted = true;
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, property));
        }
        else if (direction == ListSortDirection.Descending)
        {
            ResetItems(this.OrderByDescending(item =&gt; property.GetValue(item)).ToList());
            IsSorted = true;
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, property));
        }
    }


Basically I am using LINQ to order another collection and change its Items list with the main one.

private void ResetItems(List&lt;TItem&gt; items)
    {
        base.ClearItems();

        foreach (var item in items)
        {
            base.InsertItem(IndexOf(item), item);
        }
    }


I would like to have an external opinion on this way of sorting my collection, if they are any flaws that I would have missed, and if so, is there a way to avoid them?
The ApplySortCore() method could be made more dry  

protected virtual void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
    this.SortProperty = property;
    this.SortDirection = direction;

    IEnumerable&lt;T&gt; items = null;
    if (direction == ListSortDirection.Ascending)
    {
        items = this.OrderBy(item =&gt; property.GetValue(item));
    }
    else
    {
        items = this.OrderByDescending(item =&gt; property.GetValue(item));
    }

    ResetItems(items.ToList ());

    IsSorted = true;
    OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, property));
}  




You should always try to code against interfaces instead of a concrete implementation.  

In the ResetItems() method you should either use an IList&lt;TItem&gt; or better an IEnumerable&lt;TItem&gt; for the input parameter.  



The call to this IndexOf() method seems a little bit strange. You already have the items sorted why don't you just add the items one by one ?  

Because the methods are inherited from Collection&lt;T&gt; we can use the Add() method which simplifies the ResetItems() method to  

private void ResetItems(IEnumerbale&lt;TItem&gt; items)
{
    base.ClearItems();

    foreach (var item in items)
    {
        base.Add(item);
    }
}