2017-05-30 80 views
1

目前,我有以下结构对象的列表:按孩子元素平均排序?

public class DataPointSet { 
    public string target{ get; set; } 
    public List<List<decimal>> datapoints{ get; set; } 
} 

一切要追加到此列表是工作,但我需要的基础上,平均每单元第一指数在datapoints列表来排序。我目前的LINQ to获得平均:

datapoints.Select(innerList => innerList.First()).Average() 

有没有办法排序基于datapoints列表中的每个元素的平均的第一个元素的这个名单?

对不起,如果这是混乱,任何帮助将是伟大的,谢谢!

回答

2

由于datapoints是集合的集合,调用First()需要你传递给Average拉姆达内部进行,具体如下:

IEnumerable<DataPointSet> datapoints = ... 
var ordered = datapoints 
    .OrderBy(pointSet => pointSet.datapoints.Average(point => point.First())); 
0

为您的数据点类和覆盖比较:

public class DataPoint: IComparable<DataPoint> 
{ 
    public List<decimal> points; 

    [...] 

    public int CompareTo(DataPoint that) 
    { 
     if (this.points.Average() > that.points.Average()) return -1; 
     if (this.points.Average() == that.points.Average()) return 0; 
     return 1; 
    } 
} 

然后你可以在列表中调用Sort。