如何在C#中对升序的字符串数组进行排序,我想在C ++中使用类似std :: sort的方法:
std::sort(population.begin(), population.end())
我需要对对象列表进行排序。列表中的对象是Genome类的实例。我在该类中重载了运算符。
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);
}
}
这是声明人口的方式:
List<Genome> population = new List<Genome>();
我如何排序此数组?可以像C ++中那样使用运算符重载的运算符
最佳答案
population.OrderBy(x => x.weights);
要么:
population.OrderByDescending(x => x.fitness);
关于c# - 在C#中对对象数组进行排序(与std::sort等效),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38424626/