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 <ListBox>. 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
Reblogged from my 
Infragistics blog

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: