title: C# Lambda expression with IComparer and Array.Sort (converting from Java to C#)
body: <p>I'm trying to convert my lambda expression that uses the <code>Comparator</code> interface from Java to C#. I got the  following error:</p>
  
  <blockquote>
    <p>Cannot convert lambda expression to type
    '<code>System.Collections.IComparer</code>' because it is not a delegate type.</p>
  </blockquote>
  
  <p>I tried to switch to <code>Comparison&lt;T&gt;</code>, but it doesn't work with my <code>Array.Sort</code> methods: </p>
  
  <pre><code>Array.Sort(Population, Population[0].Comparator);
  Array.Sort(population, 0, population.Length, population[0].Comparator);
  </code></pre>
  
  <p>PregancyIndividual implements IGeneticIndividual interface.</p>
  
  <pre><code>public virtual IGeneticIndividual[] selectPopulation(IGeneticIndividual[] population)
      {
          IGeneticIndividual[] res = new IGeneticIndividual[population.Length];
          Array.Sort(population, 0, population.Length, population[0].Comparator);
  </code></pre>
  
  <p>Here is my Java code:</p>
  
  <pre><code>@Override
  public Comparator getComparator() {
      Comparator&lt;PregnancyIndividual&gt; IndividualFitnessComparator
              = (PregnancyIndividual ind1, PregnancyIndividual ind2) -&gt; {
                  int res = 1;
                  double fitness1 = ind1.getFitnessCalculator().calculateFitness(ind1);
                  double fitness2 = ind2.getFitnessCalculator().calculateFitness(ind2);
                  if (fitness1 &gt; fitness2) {
                      return -1;
                  }
                  if (fitness1 == fitness2) {
                      return 0;
                  }
                  return res;
              };
      return IndividualFitnessComparator;
  }
  </code></pre>
  
  <p><code>PregnancyIndividual</code> implementing my own interface :</p>
  
  <pre><code>public class PregnancyIndividual implements IGeneticIndividual
  </code></pre>
  
  <p>I am now trying to implement my code for C#:</p>
  
  <pre><code>public virtual IComparer Comparator
      {
         get
          {
              //&lt;PregnancyIndividual&gt;
              IComparer IndividualFitnessComparator = (PregnancyIndividual ind1, PregnancyIndividual ind2) =&gt;
              {
                  int res = 1;
                  double fitness1 = ind1.FitnessCalculator.calculateFitness(ind1);
                  double fitness2 = ind2.FitnessCalculator.calculateFitness(ind2);
                  if (fitness1 &gt; fitness2)
                  {
                      return -1;
                  }
                  if (fitness1 == fitness2)
                  {
                      return 0;
                  }
  
                  return res;
              };
  
  
              return IndividualFitnessComparator;
          }
      }
  </code></pre>