-9

I have a list with a shed load of data points and a lot of them are the same as each other. Is there a convenient command to sort the list to see what occurs the most?

I'm writing the results to a text file, so I can actually see the results of the list?

BugFinder
  • 17,474
  • 4
  • 36
  • 51
Hunce1947
  • 3
  • 2

1 Answers1

0
public class SomeObject
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

        private static void ListCount()
        {
            var items = new List<SomeObject>
            {
                new SomeObject {FirstName = "santa", LastName = "claus"},
                new SomeObject {FirstName = "fred", LastName = "claus"},
                new SomeObject {FirstName = "tooth", LastName = "fairy"},
                new SomeObject {FirstName = "easter", LastName = "bunny"},
            };

            var byLastNameFrequency = items.GroupBy(i => i.LastName).OrderByDescending(g => g.Count()).ThenBy(g => g.Key);
            foreach (var name in byLastNameFrequency)
            {
                Console.WriteLine(name.Key + ", " + name.Count());
            }
        }
Jay Allard
  • 938
  • 6
  • 12