Sorting an observable collection with linq

26,064

Solution 1

If you are using Silverlight 3.0, then using CollectionViewSource is the cleanest way. Refer below example: (it can be done via xaml as well)

ObservableCollection<DateTime> ecAll = new ObservableCollection<DateTime>();
CollectionViewSource sortedcvs = new CollectionViewSource();
sortedcvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Date", 
    System.ComponentModel.ListSortDirection.Ascending));
sortedcvs.Source = ecAll;
ListBoxContainer.DataContext = sortedcvs;

And in corresponding xaml set

ItemsSource="{Binding}"

for the ListBox or any ItemsControl derived control

Solution 2

Since the collection doesn't provide any Sort mechanism, this is probably the most practical option. You could implement a sort manually using Move etc, but it will probably be slower than doing in this way.

    var arr = list.OrderBy(x => x.SomeProp).ToArray();
    list.Clear();
    foreach (var item in arr) {
        list.Add(item);
    }

Additionally, you might consider unbinding any UI elements while sorting (via either approach) you only pay to re-bind once:

Interestingly, if this was BindingList<T>, you could use RaiseListChangedEvents to minimise the number of notifications:

    var arr = list.OrderBy(x => x).ToArray();
    bool oldRaise = list.RaiseListChangedEvents;
    list.RaiseListChangedEvents = false;
    try {
        list.Clear();
        foreach (var item in arr) {
            list.Add(item);
        }
    } finally {
        list.RaiseListChangedEvents = oldRaise;
        if (oldRaise) list.ResetBindings();
    }

Solution 3

Note that in Linq, you are given an IEnumerable from your query, and that query has not executed yet. Therefore, the following code only runs the query once, to add it to an ObservableCollection:

var query = from x in Data
            where x.Tag == "Something"
            select x;

foreach(var item in query)
    MyObservableCollection.Add(item);

Take a look at the "OrderBy" extension on IEnumerable:

foreach(var item in query.OrderBy(x => x.Name))
    MyObservableCollection.Add(item);

Solution 4

ObservableCollections aren't designed to be sortable. List is sortable, and that's the underlying mechanism used by the answer referencing List.Sort(), but ObservableCollection isn't derived from List so you're out of luck there. Imo, the "right" solution is not to try to sort the ObservableCollection, but to implement ICollectionView and bind an instance of that to your control. That interface adds methods for sorting and has the additional benefit that its recognized by Silverlight controls (well, the ones that support it anyway such as DataGrid) so your sorting could be utilized directly from the UI layer. This question might be helpful:

Silverlight and icollectionview

Share:
26,064
Xander
Author by

Xander

Updated on July 09, 2022

Comments

  • Xander
    Xander about 2 years

    I have an observable collection and I sort it using linq. Everything is great, but the problem I have is how do I sort the actual observable collection? Instead I just end up with some IEnumerable thing and I end up clearing the collection and adding the stuff back in. This can't be good for performance. Does anyone know of a better way to do this?

  • laznik
    laznik almost 15 years
    I used this as a base for something similar for my code. I'm starting to think Linq is pretty kick-ass :)
  • Jack
    Jack almost 14 years
    you must raise a property changed event on the Terms-property
  • VoodooChild
    VoodooChild almost 14 years
    I am not sure how well this would work if you are getting the ObservableCollection from the WCF service, any advice? Do I need to create a new ObservableCollection on the client and apply "OrderBy" to it? - thanks.
  • Brian Genisio
    Brian Genisio almost 14 years
    @VoodooChild: If you already have an ObservableCollection instead of just an IEnumerable, then look at the SO question: stackoverflow.com/questions/996126/…
  • JoeBrockhaus
    JoeBrockhaus about 10 years
    If you're going to go this route, for performance, I'd use var ordered = list.OrderBy(x => x.SomeProp); list.Clear(); list.AddRange(ordered); You'll avoid the unnecessary iteration & execution of ToArray() and only do so once during AddRange(). (I'd have to decompile to verify this, since AddRange takes an IEnumerable, it may even just store that without executing it, and then execute & store the flattened collection before any List-specific operations are called. Same performance in the end, but it just defers the cycle hit until later.)