Assuming you have a List<string>
that contains your items:
List<string> myList = PopulateList(...); // fill the list with names
If you just want to sort the list, you can use List.Sort
:
myList.Sort();
You could then go through the list with a loop to count up the duplicates and determine which is most frequent.
Or, you can group the items and then sort the grouping by count, descending, and take the first item.
var mostCommon =
myList.GroupBy(x => x)
.OrderByDescending(g => g.Count())
.First();
And then mostCommon.Key
will tell you the most common item, and mostCommon.Count()
will tell you how many items there are.
Or, if you wanted a list of {name, count}
, ordered by count:
var groupedAndSorted =
myList.GroupBy(x => x)
.Select(x => new {Key=x.Key, Count=x.Count()})
.OrderByDescending(x => x.Count)
.ToList();