Possible Duplicate:
LINQ identity function?
It seems wasteful to have to type x => x
just to sort something like ints or strings... is there a quicker way?
if you have List<T>
you can use Sort
method: MyList.Sort()
, or for other types may be you can find similar functions.
but by Enumerable.OrderBy
as MSDN link says, No there isn't anyway.
static void Main()
{
var array = new[] { 3, 2, 1 };
var result = array.OrderBy(SimpleSort);
foreach (var item in result)
{
Console.WriteLine(item);
}
}
public static T SimpleSort<T>(T t)
{
return t;
}
or create own extension:
public static class Extensions
{
public static IEnumerable<TSource> OrderBy<TSource>(this IEnumerable<TSource> source)
{
return source.OrderBy(t => t);
}
}
精彩评论