Search code examples
c#ranking

Show most occured in rank order #


I have a list consisting of duplicates, I want to create an output showing them in rank. e.g tom at the top because he has 100 entries and lilly at the bottom because it has 0.

is this possible to display a list with a rank corosponding to the number of times they appear in the list?

also display the number of times they appear next to them?


Solution

  • var query = File
        .ReadLines("input.txt")
        .GroupBy(x => x)
        .Select(g => new { Key = g.Key, Count = g.Count() })
        .OrderByDescending(i => i.Count)
        .Take(20);
    
    foreach (var item in query)
    {
        Console.WriteLine("{0,5} {1}", item.Count, item.Key);
    }