問題

我有一個列表:

 dave
maggie
john
stuart
john
john
dave
john
maggie
maggie
 

我想要的結果是:

 john
john
john
john
maggie
maggie
maggie
dave
dave
stuart
 

  最佳答案

首先我對它們進行分組.然後按每個組的計數排序.最後使用SelectMany從組中的每個名稱獲得一個平坦的結構.

 var myList = new List<string>()
{
    "dave",
    "maggie",
    "john",
    "stuart",
    "john",
    "dave",
    "john",
};

var result = myList
    .GroupBy(x => x)
    .OrderByDescending(x => x.Count())
    .SelectMany(x => x)
    .ToList();
 

  相同標籤的其他問題

c#arrayslistlinqsorting