How to

sort ObservableCollection

Published: 1. November 2011 | Updated: 1. November 2011
License: Microsoft Public License (MS-PL)
Categories: Collections » Linq
Tags: C# Collections
Was this snippet helpful for you? YESYES / NONO

Import namespaces

using System.Collections.ObjectModel;
using System.Linq;

Code

var collection = new ObservableCollection<int>();
            
collection.Add(7);
collection.Add(4);
collection.Add(12);
collection.Add(1);
collection.Add(20);

// ascending
collection = new ObservableCollection<int>(collection.OrderBy(a => a));

// descending
collection = new ObservableCollection<int>(collection.OrderByDescending(a => a));
Send us feedback about this snippet »



Related Snippets: