4

I am reading through a book and it gives an example of the Sort Method along with a Lambda query.

An example is {Product.Sort( (x,y) => x.Name.CompareTo(y.Name) );

This really took me a while to understand as I did not understand how .Sort was dealing with the two inputs on the lambda.

I tried clicking on Sort and pressing F1 for help, but, it didn't give anything, that made any sense to me.

Perhaps I am just not good enough to understand those examples, but, I just could not work out how this was working until I changed the Lambda to x,y,z which gave the error Error Delegate 'System.Comparison<ConsoleApplication1.Product>' does not take 3 arguments

Which made a lot more sense to me... Anyway, after a while of looking around, I am confident that I understand the Sort method, but, it took me a lot longer than I am happy with.

From people who are much better than me - given a situation like this, how would you search for help?

By typing Shift+SpaceI was also able to produce the following:

enter image description here

However, I am just wondering, as a C# learner, how can I attribute this to requiring a Lambda with two inputs?

wil
  • 2,019
  • 2
  • 15
  • 14
  • Look up List.Sort on Google and then Google it's arguments if you don't understand. That's the way most dev's work these days – Tony The Lion Sep 07 '11 at 18:37
  • take a look at [this SO question](http://stackoverflow.com/questions/274022/how-do-i-pronounce-as-used-in-lambda-expressions-in-net/274025#274025). It may not completely answer your question but the explanation might help you to understand what's going on. – Eonasdan Sep 07 '11 at 18:37

5 Answers5

5

It does not require a lambda. It requires only a delegate of type Comparison. For example, there may be a method:

int CompareProductNames(Product x, Product y)
{
    return x.Name.CompareTo(y.Name);
}

and then you could call Sort like this:

productList.Sort(CompareProductNames);

Comparison<T> is a delegate that takes two parameters of type T and returns an int.

pnvn
  • 488
  • 1
  • 5
  • 11
1

It sounds like you don't need an explanation of what's going on but instead how to figure it out. Hover your mouse over the Sort part and you'll see the specific overload being called. In this case its Sort(Comparison<Product>)) which maps to Sort(Comparison(T))

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
1

One of the approaches is to check out the Comparison documentation. It's a delegate, e.g. pointer to a function, that takes two arguments of type T and returns integer value. T type is declare in Sort<T> method of the List<T> class, so T here is a Product. Lambda expressions is just a shortcut for this function.
The main point is to understand that this code is just an equivalent of:

    public void DoSort()
    {
        list.Sort(Compare); //Pass method as a Comparison<Product>
    }

    public int Compare(Product x, Product y)
    {
        return x.Name.CompareTo(y.Name);
    }
default locale
  • 13,035
  • 13
  • 56
  • 62
0

Look up List<T>.Sort on Google and then Google it's arguments if you don't understand. That's the way most dev's work these days.

I, for one, could not write code without the help of Google and sites like SO itself.

Also, you have to understand how delegates work, in the case of sort, as the lambda is really an anonymous method that is passed to the delegate. The input's are captured by the lambda, are arguments to the delegate.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
0

It might be easier to understand the MSDN doc that comes up with F1 if you know that a lambda evaluates to a single delegate value. The MSDN doc shows two overloads for Sort with a single argument; clicking on the argument type of these will show that Comparison is a delegate taking two arguments.

You do have to drill in a few steps in the MSDN doc to find the information you are looking for.

antlersoft
  • 14,636
  • 4
  • 35
  • 55