Sorted lists in Windows 8 applications

While developing Windows 8 applications I have come across the requirement to have my list of items sorted on my screen, sometimes in a . Unfortunately the base WinRT controls make this really difficult to achieve.

After a few hours of searching for an easier way to do it, I finally discovered a way of sorting the items. Rather than having the controls automatically sort for me (as I was used to doing in Silverlight, etc.), but instead by sorting the underlying list itself. It makes total sense in hindsight.

A blog post from Andrea Boschin explains how to do it: http://www.xamlplayground.org/post/2010/04/27/Keeping-an-ObservableCollection-sorted-with-a-method-override.aspx

The sorting can be achieved automatically by overriding a method in the ObservableCollection. The final step is to add the IComparable interface to the class you want to sort.

Here is an example IComparable implementation that will sort alphabetically

public int CompareTo(object obj)
{
    var person = obj as Person;
    return string.CompareOrdinal(this.Name, person.Name);
}

By David Burela


Tags /

Comments  (1 )

Brian Lagunas
on Mon, Dec 31 2012 7:04 PM

Here is another approach that may work as well:

brianlagunas.com/.../write-a-sortable-observablecollection-for-wpf

Add a Comment

Please Login or Register to add a comment.