14

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……

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Pritesh
  • 3,208
  • 9
  • 51
  • 70
  • 2
    Why do you want to sort the collection? You can also perform the sorting on the data binding. – PVitt Apr 27 '11 at 12:06
  • [In my applicaton “Item controle” display each employee in observable collection now I want to sort observable collection so UI of my application will change as per observable collection will be sorted……thanks] – Pritesh Apr 27 '11 at 12:14

4 Answers4

27

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}}"
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
PVitt
  • 11,500
  • 5
  • 51
  • 85
  • 1
    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.` – Ondrej Janacek May 25 '14 at 14:42
  • @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. – PVitt May 26 '14 at 07:25
  • @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. – Ondrej Janacek May 26 '14 at 09:45
  • @OndrejJanacek Anyway... Thanks for the edit. – PVitt May 26 '14 at 10:28
  • How can I do this descending? – devjme Sep 17 '19 at 18:38
  • @devjme https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.sortdescription?view=netframework-4.8 – PVitt Sep 18 '19 at 10:47
  • awesome thx!!!! – devjme Sep 18 '19 at 15:48
3

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

http://softcollections.codeplex.com/

jonchicoine
  • 510
  • 3
  • 14
2

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

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
1

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

Daniel Rose
  • 17,233
  • 9
  • 65
  • 88