reactiveui/ReactiveUI — pull request #216 by niik (2013-03-20 13:43:23+00:00)
Title: Simplified way of writing ordering functions

When creating derived collection you need to provide an ordering function. Writing these comparators is easy enough when you only need to deal with a single property, ie ```(x,y) => x.Show.CompareTo(y.Show))```. But they can get complex in a hurry if you need to take multiple properties into account. The OrderedComparer helps with that by adding some syntactic sugar for chaining multiple comparisons together.

Say you have a list of employees (yup, I have zero imagination) and you wish to sort these by descending Salary, descending Age and ascending Name (ignoring case). Today you'd probably write something like this:

```c#
int CompareEmployees(Employee x, Employee y) {
int ret = 0;

ret = -(x.Salary.CompareTo(y.Salary));

if(ret != 0) {
return ret;
}

ret = -(x.Age.CompareTo(y.Age));

if(ret != 0) {
return ret;
}

return StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name);
}
```

With OrderedComparer you'd write that like this:

```c#
OrderedComparer
.OrderByDescending(x => x.Salary)
.ThenByDescending(x => x.Age)
.ThenBy(x => x.Name, StringComparer.OrdinalIgnoreCase);
```

LINQ-style chaining. The end result is a reusable generic comparer, an IComparer instance which you can use for sorting not only derived collections but also generic lists or any other type which relies on ICompare.

The OrderedComparer class is only syntactic sugar in itself since the ThenBy and ThenByDescending methods are implemented as extension methods on the IComparer interface. The OrderedComparer only exists to provide a nice starting point but you could also start from an existing comparer.

```c#
// This is a stupid comparer
StringComparer.OrdinalIgnoreCase
.ThenBy(x => x, StringComparer.Ordinal);
```

## Anonymous types
There's a convenience method on the non-generic OrderedComparer, ```.For(IEnumerable enumerable)``` that can be used to create comparer for anonymous types. Check the tests for examples.