Sorting ObservableCollection

14,977

Solution 1

You can sort the view of the collection rather that sorting the collection itself:

// xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
<myView.Resources>
    <CollectionViewSource x:Key="ItemListViewSource" Source="{Binding Itemlist}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="SortingProperty" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</myView.Resources>

And then you can use the CollectionViewSource as ItemSource:

ItemsSource="{Binding Source={StaticResource ItemListViewSource}}"

Solution 2

I think PVitt may have the best solution... however, i did find this SortedObservableCollection class that perhaps could help?

http://softcollections.codeplex.com/

Solution 3

I implemented an ObservableCollectionView which supports sorting and filtering using a lambda (like LINQ but live) and item tracking:

https://mytoolkit.codeplex.com/wikipage?title=ObservableCollectionView

Solution 4

You don't need to sort yourself, but can let WPF do it for you. See SortDescription, for example.

Share:
14,977
Pritesh
Author by

Pritesh

Updated on June 07, 2022

Comments

  • Pritesh
    Pritesh about 2 years

    Suppose I have ObservableCollection of employee class

    public ObservableCollection<Employee> employeeCollection = new ObservableCollection<Employee>();
    
    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public double MobileNumber { get; set; }
        public string City { get; set; }
        public int Age { get; set; }
    
        public Employee() {}
    }
    

    now I am trying to sort the ObservableCollection (“employeeCollection”) by appropriate selection by user from combobox[it will be….Sort By FirstName….Sort By MobileNumber etc…]..

    and it is required to get back sorted observable collection…. Means it should not be in form of “var” it should be ObservableCollection<Employee>

    So I can assign back it to “ItemsSource” property of “ItemsControl”

    Thanks……

  • Roman Starkov
    Roman Starkov about 12 years
    An alternative implementation of a sorted observable collection: ObservableSortedList<T>.
  • Ondrej Janacek
    Ondrej Janacek about 10 years
    Note that PropertyName can't use binding. It directly results in the following run-time error: A 'Binding' cannot be set on the 'PropertyName' property of type 'SortDescription'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
  • PVitt
    PVitt about 10 years
    @OndrejJanacek Is this new behaviour? I could swear I used it that way. But I do not have access to the code anymore to check it.
  • Ondrej Janacek
    Ondrej Janacek about 10 years
    @Well, it's not highly propable that you used it that way. The property is after all called PropertyName which indicates that it could take a string name of a property, not a direct binding to it. But I'm new to WPF, I only stumbled across this because I was searching for the solution and I implemented it, so it's maybe possible that it worked the other way before.
  • PVitt
    PVitt about 10 years
    @OndrejJanacek Anyway... Thanks for the edit.
  • sa.he
    sa.he over 5 years
    Codeplex link is broken
  • devjme
    devjme almost 5 years
    How can I do this descending?
  • PVitt
    PVitt almost 5 years
  • devjme
    devjme almost 5 years
    awesome thx!!!!