Break Down
- TSource: This is the type of elements in the collection which need to be ordered
- TKey: The type key by which the elements are ordered.
Func<TSource,TKey>
: Delegate which will return a key for a given element in the collection
This function is essentially a sorting function. As such it needs a way to compare the elements in the collection. This particular method assumes that for a given object there is a corresponding key value by which they can be sorted.
Take for example the following class Student
class Student {
string Name { get; set; }
...
}
If I wanted to sort a collection of Student
instances by their name I could do the following
IEnumerable<Student> col = GetTheStudents();
var ordered = col.OrderByDescending( x => x.Name );
In this case the values would be as follows
- TSource:
Student
- TKey:
String
Func<TSource,TKey>
: This is the passed in lambda expression x => x.Name