'Order by duplicate amount in a list using linq

I have this class:

class Cell
{
    string keyword;
    double bkgColor;
    double keywordColor;
}

I have a list of this class and I want to order it by duplicates of bkgColor using linq. For example: Keywords with background color that has the most keywords assigned, would be at the start of the list etc.

Evey answer is highly appreciated!



Solution 1:[1]

Assuming that keyword, bkgColor and keywordColor are public properties of the class Cell. You can use this linq

var result = cells.GroupBy(c => c.bkgColor)
            .OrderByDescending(g => g.Count())
            .SelectMany(g => g.Select(c => c));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1