2
var CustomStatus = new[] { "PAG", "ASG", "WIP", "COMP", "SEN" };

List<CDSHelper> HelperList = new List<CDSHelper>();
// Getting the values from API to fill the object and
// finally doing the custom order by

var result = HelperList.OrderBy(a => Array.IndexOf(CustomStatus, a.status));

カスタム オーダーを使用して HelperList オブジェクトを並べ替えています。合計で約 18 のステータスがあります。18 のステータスのうち、CustomStatus に基づいてリストを並べ替えたいのですが、残りの順序は CustomStatus ステータスの後にリストに追加する必要があります。上記のコードを使用するHelperList の最後で CustomStatus を取得できます。これを達成する方法は?

4

2 に答える 2

3

OrderByそれを行う最も簡単な方法はおそらく使用することですが、リストにないアイテムが最後になるように、アイテムが存在しない場合に返される値をより高い値ThenByに変更する必要があります。-1 IndexOf

var result = HelperList.OrderBy(a => {
                         var x = Array.IndexOf(CustomStatus, a.status);
                         if(x < 0)
                            x = int.MaxValue;
                         return x;
                     }).ThenBy(a => a.status); //Sort alphabetically for the ties at the end.

CustomStatus別の方法は、使用する順序を逆にすることですOrderByDecending

var CustomStatus = new[] { "SEN", "COMP", "WIP", "ASG","PAG" };

List<CDSHelper> HelperList = new List<CDSHelper>();
// Getting the values from API to fill the object and
// finally doing the custom order by

var result = HelperList.OrderByDecending(a => Array.IndexOf(CustomStatus, a.status))
                       .ThenBy(a.status);
于 2013-10-05T05:46:54.510 に答える