I have a List<string> _words like
"Car", "Car", "Car", "Bird", "Sky", "Sky"
I want to sort it by each word count descending so the final List<string> would be
"Car",
"Sky",
"Bird
how do I do this in LINQ? I don't really need the count for each word
in SQL this would be:
select word, count(1) as count1
from word
group by word
order by count1 desc, word
Another variant:
    var _output = from p in _words
                  group p by p into g
                  orderby g.Count() descending, g.Key ascending 
                  select g.Key;
                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();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With