0

I want order to object array by one of its variables "n_common" and this gives me error:

you can´t convert Lambda expressions in the type array because is not a type delegate.

cartesian_product.Sort((a, b) => -1 * a.n_common.CompareTo(b.n_common));

I want to order descending my "Pair" object that contains 3 variable "p", "q" and "n_common", it should be ordered by the "n_common", the cartesian_product is a array of Pair object.

I am not working with Linq just with Lambda expression or at least that is how I understand it anyway I put the linq in the header and the problem continues

CoolLife
  • 1,419
  • 4
  • 17
  • 43
  • No because I am not working with Linq just with Lambda expression or at least that is how I understand it anyway I put the linq in the header and the problem continues – CoolLife Mar 10 '19 at 22:43
  • What is the type of cartisian_product? What do you expect your lambda to do. I believe that Sort is expecting a delegate that takes an instance of the type the collection is collecting and return a value that can be sorted. Something like `people.Sort(p => p.Age)` would take a collection of persons (in a variable *people* and sort them by age. In your case, the delegate is specifying a function that takes two parameters). – Flydog57 Mar 10 '19 at 23:18

2 Answers2

0

At the top of your class add using System.Linq; and using System.Data.Entity;.

user1666620
  • 4,800
  • 18
  • 27
  • No because I am not working with Linq just with Lambda expression or at least that is how I understand it anyway I put the linq in the header and the problem continues – CoolLife Mar 10 '19 at 22:43
  • The various extension methods that take a lambda expression (for example `Sort`) are defined in `System.Linq`. You may not think you are using LINQ, but you are. – Flydog57 Mar 10 '19 at 23:10
0

Sort is a static method, so it must be called from the class Array not an instance. Try this:

Array.Sort(cartesian_product, (a, b) => -1 * a.n_common.CompareTo(b.n_common));
Amir Molaei
  • 3,700
  • 1
  • 17
  • 20