I have a list of lists, with each inner list being a list of integers like this (code is simplified for clarity):
var listOfLists = new List<List<int>>
{
new List<int> {6, 0, 2, 5, 6}, // #3 in sort order
new List<int> {6, 0, 2, 2, 5, 6}, // #2 in sort order
new List<int> {0, -1, 0, 0, 7}, // #1 in sort order
new List<int> {11, 3, 5, 5, 12}, // #4 in sort order
};
I want to sort these lists using LINQ-to-Objects according to the following rules:
- If either list is empty, the non-empty list sorts first
- The list with the lowest minimum number is sorted first
- If there's a tie, the largest number of duplicates of the minimum is sorted first. Example:
{2,2}
sorts before{2}
. - If there's still a tie, remove the minimums from both lists and goto Step #1
Background:
Each list represents locations which each have 1+ retail stores. Each retail store is assigned an "urgency" number defining how badly that location needs to be restocked. The "worst" is -1, which usually means a customer has complained about empty products. 0 usually means the store has run out of high-selling products. 1 is usually a store that's almost empty. And so on. In practice the urgengy calculation is complex and based on multiple criteria validated by data-mining, but how we get the numbers isn't important for the sorting.
I want to find the most urgent locations, which I'm defining as the location with the most urgent store, with the number of stores at that level of urgency used as a tie-breaker.