I have an string array containing names like:
[Alex, Alex, Michael, Michael, Dave, Victor]
That I convert to a List<string>
Then I need to write a function that returns the max repeated item in the list but should be sorted in descending order, which in this case, Michael.
I have followed the LINQ code stated in this link. Which is:
string maxRepeated = prod.GroupBy(s => s)
.OrderByDescending(s => s.Count())
.First().Key;
However, code returns Alex instead of Michael.
I tried to add another OrderByDescending
however it returns Victor.
string maxRepeated = prod.GroupBy(s => s)
.OrderByDescending(s => s.Count())
.OrderByDescending(b => b)
.First().Key;
I am stuck and don't know what needs to be added to achieve the desired result.
Any help is appreciated.
Not a second OrderByDescending
which ignores the previous order but ThenByDescending
:
string maxRepeated = prod.GroupBy(s => s)
.OrderByDescending(g => g.Count())
.ThenByDescending(g => g.Key)
.First().Key;
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