[英]sort list with C# by how often same strings appear in list
I have a list:我有一个清单:
dave
maggie
john
stuart
john
john
dave
john
maggie
maggie
My desired result would be:我想要的结果是:
john
john
john
john
maggie
maggie
maggie
dave
dave
stuart
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.最后使用
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.