Reputation: 275
I have a dictionary, it's key is a string, and the value is a list of objects. I want to sort its keys based on 2 factors, the size of the list, and the object Id. The values are sorted so the first element in the list is always the lowest id.
So far I managed to use order by to sort it by the number of elements in the list, but now I am having a hard time sorting based on the other factor.
Example: Dictionary X contains current keys/values
["A",{2,4,5,6}]
["B",{6}]
["C",{1}]
["D",{0,1}]
["E",{0,99,88,66}]
After sorting it should be
["E",{0,99,88,66}]
["A",{2,4,5,6}]
["D",{0,1}]
["C",{1}]
["B",{6}]
Here is what I have:
Public MyObject{
public Int Id{ get; set;}
}
var dictionary = new Dictionary<string, List<MyObject>>();
var dictionarySortedByCount = dictionary.OrderBy(x => x.Value.Count).Reverse().ToDictionary(x => x.Key, x => x.Value);
Any help would be appreciated.
Upvotes: 1
Views: 2175
Reputation: 4002
Have you tried adding another order condition after the first one like below ?
var dictionarySortedByCount = dictionary
.OrderByDescending(x => x.Value.Count)
.ThenBy(x => x.Value[0].ObjectId)
.ToDictionary(x => x.Key, x => x.Value);
Upvotes: 3