1

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?

Community
  • 1
  • 1
Oli
  • 11
  • 1

2 Answers2

4

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.

Saeed Amiri
  • 21,361
  • 5
  • 40
  • 81
4
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);
    }
}
Kirill Polishchuk
  • 51,053
  • 10
  • 118
  • 119