我有一个泛型列表,其中包含一个属性(类类型)。我需要一个针对Z参数(TrainingSet)的排序方法:
public override List<TrainingSet> CalculatedDistancesArray
(List<TrainigSet> ts, double x, double y, int k)
{
for (int i =0; i < ts.Count; i++)
{
ts[i].Z = (Math.Sqrt(Math.Pow((ts[i].X - x), 2)
+ Math.Pow((ts[i].Y - y), 2)));
}
// I want to sort according to Z
ts.Sort(); //Failed to compare two elements in the array.
List<TrainingSet> sortedlist = new List<TrainingSet>();
for (int i = 0; i < k; i++)
{
sortedlist.Add(ts[i]);
}
return ts;
}
public class TrainigSet
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public string Risk { get; set; }
}