0

I am trying to sort a ObservableCollection using a extension method.

I'm using as reference this post: https://stackoverflow.com/a/16344936/1357553

Here is my code:

public static void Sort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
{
    List<T> sorted = collection.OrderBy(x => x).ToList();
    for (int newIndex = 0; newIndex < sorted.Count(); newIndex++)
    {
        int oldIndex = collection.IndexOf(sorted[newIndex]);
        if (oldIndex != newIndex)
            collection.Move(oldIndex, newIndex);
    }
}

So, when I call this method using I got an exception:

ObservableCollection<myDataModel> AllTasks = new ObservableCollection<myDataModel>();
//fill AllTasks 
this.AllTasks.Sort();

Exception:

"An item with the same key has already been added."

The exception occurs in when I call the Move() method.

The funny thing is that, when I use the same method with a "string" instead of "myDataModel", it works fine. I have googled all over and I could not find anything about this exception. It looks like there is something wrong with "myDataModel" implementation.

EDIT:

The implementation of myDataModel is quite ordinary.

public class myDataModel : IComparer<myDataModel >, IComparable<myDataModel >
{
    //other stuff

    public bool Equals(myDataModel other)
    {
        //equals implementation
    }


    #region IComparer, IComparable
    public int Compare(MainPlanTGanttTaskViewModel t1, MainPlanTGanttTaskViewModel t2)
    {
        return t1.CompareTo(t2);
    }

    public int CompareTo(MainPlanTGanttTaskViewModel other)
    {
        if (this.Start.CompareTo(other.Start) != 0) return this.Start.CompareTo(other.Start);
        else
        {
            return this.SapCode.CompareTo(other.SapCode);
        }
    }
    #endregion

}

Can anyone give me some help?

Community
  • 1
  • 1
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
  • Well...what's myDataModel implementation? Equals(), GetHashCode(), stuff like that... – Adriano Repetti Jun 23 '14 at 16:16
  • "It looks like there is something wrong with "myDataModel" implementation.", so..would you like to show us your implementation that might be faulty? – Patrick Jun 23 '14 at 16:16
  • cant OrderBy be enough to sort? – Mo Patel Jun 23 '14 at 16:20
  • 3
    Instead of sorting the observablecollection, have you looked into CollectionViewSource? It has most of the functionality you need for a view (like sorting and filtering), but leaves your initial collection intact. – Patrick Jun 23 '14 at 16:20
  • you migth wanna see this example - http://blogs.msdn.com/b/priozersk/archive/2010/05/26/a-few-useful-extensions-for-observablecollection.aspx – terrybozzio Jun 23 '14 at 16:21
  • @M Patel, I am avoiding replacing the collection. – guilhermecgs Jun 23 '14 at 16:24
  • @terrybozzio, I have tried this example, but the code does not compile – guilhermecgs Jun 23 '14 at 16:27
  • then see this one - http://stackoverflow.com/questions/1945461/how-do-i-sort-an-observable-collection – terrybozzio Jun 23 '14 at 16:31
  • If you are using WPF with a DataGrid or a ListView, the right solution is indeed to use CollectionViewSource - it supports sorting out of the box. But you may still have a problem with your Compare implementation. – igelineau Jun 23 '14 at 20:03

1 Answers1

0

I'm using below code to sort Collection:

ConceptItems = new ObservableCollection<DataConcept>(ConceptItems.OrderBy(i => i.DateColumn));

Simple & Easy

4est
  • 3,010
  • 6
  • 41
  • 63