Sorting for an ObservableCollection

I am porting my Live Countdown app to Windows 8 in order to help my learn Windows 8 and to spread the goodness to more people 🙂

However one problem I had was sorting an ObservableCollection without causing it to reset. I got round this issue by implementing the QuickSort algorithm as an extension method so I could do in place sorting on the collection.

Here is the code:

public static class QuickSort
{

	public static void InplaceQuickSort(this ObservableCollection collection, Comparison comparison)
	{
		Sort(collection, 0, collection.Count - 1, comparison);
	}

	private static void Sort(IList array, int left, int right, Comparison comparison)
	{
		int pivot, l_hold, r_hold;
		l_hold = left; r_hold = right;
		T pivotValue = array[left];
		while (left < right)
		{
			while ((comparison(array[right], pivotValue) >= 0) && (left < right))
			{
				right--;
			}
			if (left != right)
			{
				array[left] = array[right];
				left++;
			}
			while (comparison(array[left], pivotValue)  pivot)
			Sort(array, pivot + 1, right, comparison);
	}
}
Advertisements
This entry was posted in Utility, windows8 and tagged , , , . Bookmark the permalink.

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