Linq group string array by count and sort
You'll need to use a combination of GroupBy
and OrderByDescending
:
string[] words = {"Car", "Car", "Car", "Bird", "Sky", "Sky"};
var output = words
.GroupBy(word => word)
.OrderByDescending(group => group.Count())
.Select(group => group.Key);
You can use GroupBy()
then OrderByDescending()
to order by number of occurrence starting from the most frequent :
var result = _words.GroupBy(x => x)
.OrderByDescending(x => x.Count())
.Select(x => x.Key)
.ToList();