用C#对列表中的相同字符串出现的频率进行排序

Luk*_*nas 1 c# linq arrays sorting list

我有一个清单:

dave
maggie
john
stuart
john
john
dave
john
maggie
maggie
Run Code Online (Sandbox Code Playgroud)

我想要的结果是:

john
john
john
john
maggie
maggie
maggie
dave
dave
stuart
Run Code Online (Sandbox Code Playgroud)

404*_*404 6

First I group them. Then order them by the count from each group. Lastly use SelectMany to get a flat structure from each individual name in the groups.

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();
Run Code Online (Sandbox Code Playgroud)