2017-07-10 11 views
-1

フレキシブルな配列に応じて動的なフィールド数でソートを行う必要があります。私は現在、配列の最大インデックスを使用して問題を解決しましたが、配列の実際のサイズに応じてこれを解決するほうがよかったでしょう。LINQ:動的なフィールド数による複数の並べ替え

は今のところ、それは次のようになります。

outList.Items = outList.Items.OrderByDescending(x => x.PointsOverall) 
       .ThenByDescending(x => x.Results[7].PointValue) 
       .ThenByDescending(x => x.Results[6].PointValue) 
       .ThenByDescending(x => x.Results[5].PointValue) 
       .ThenByDescending(x => x.Results[4].PointValue) 
       .ThenByDescending(x => x.Results[3].PointValue) 
       .ThenByDescending(x => x.Results[2].PointValue) 
       .ThenByDescending(x => x.Results[1].PointValue) 
       .ThenByDescending(x => x.Results[0].PointValue) 
       .ThenBy(x => x.Results.Last().FullName).ToList(); 

ソート以内に0まで行くの最後の要素から始まる減算カウンタのようなものを含める方法はありますか?

ありがとうございます。

答えて

3

その配列は常に同じ長さを持っていると仮定:

int length = outList.Items.First().Results.Length; 
var items = outList.Items.OrderByDescending(x => x.PointsOverall); 
for(int i = length - 1; i >= 0; i--) 
    items = items.ThenByDescending(x => x.Results[i].PointValue); 
items = items.ThenBy(x => x.Results.Last().FullName).ToList(); 

長さは、あなたが最初のmin-長さを計算する必要が変更した場合:

int length = outList.Items.Min(x => x.Results.Length); 
関連する問題