Applying the strategy pattern, you'd have an ISortStrategy
interface, and then several implementations like SortById
, SortByName
and SortByAge
. The interface, and its implementations, would have a method like object Sort(Animal animal);
that returns one of the animal's properties.
You'd then just select the proper strategy at runtime, and use it like this:
return from animal in ctx.Animals
orderby sortStrategy.Sort(animal) descending
select animal;