在C#中对对象数组进行排序(与std :: sort等效)

use*_*772 1 c# sorting

如何在C#中对升序的字符串数组进行排序,我想在C ++中使用类似std :: sort的方法:

 std::sort(population.begin(), population.end())
Run Code Online (Sandbox Code Playgroud)

我需要对对象列表进行排序。列表中的对象是Genome类的实例。我在该类中重载了运算符<和operator >

 class Genome
{
    public List<double> weights;
    public double fitness;

    public Genome()
    {
        fitness = 0.0;
        weights = new List<double>();
    }

    public Genome(List<double> weights, double fitness) {
        this.weights = weights;
        this.fitness = fitness;
    }


    public static bool operator <(Genome lhs, Genome rhs)
    {
        return (lhs.fitness < rhs.fitness);
    }

    public static bool operator >(Genome lhs, Genome rhs) {
        return (lhs.fitness > rhs.fitness);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是声明人口的方式:

List<Genome> population = new List<Genome>();
Run Code Online (Sandbox Code Playgroud)

我如何排序此数组?可以像C ++中那样使用运算符重载的运算符<吗?

小智 5

population.OrderBy(x => x.weights); 
Run Code Online (Sandbox Code Playgroud)

要么:

population.OrderByDescending(x => x.fitness); 
Run Code Online (Sandbox Code Playgroud)