Sort a list based on another list followed by a property on itself with LINQ
c#, dictionary, linq, list, sorting
Solution
If i have understood you correctly:
var result = unsortedFlights
.OrderByDescending(f => totalCityFilters[f.DestinationID])
.ThenBy( f => f.FinalPrice)
.ToList();
If the value of `totalCityFilters` is the number you want to order by first.
Problem
The following dictionary represents the key/value pair of a destination city ID and the number of filters associated to that city: ``` Dictionary<int, int> totalCityFilters ``` This dictionary is sorted by descending number of filters and it's the way I need it to be. I then have an unsorted list of flights: ``` List<Flight> unsortedFlights ``` Each `Flight` object has a bunch of properties where the most relevant to my problem are `DestinationID` and `FinalPrice`. I need to create a new list of flights where they are sorted based on the following 2 premisses: - The list should first be sorted based on the order of `totalCityFilters`, the key on that dictionary matches the property `DestinationID`. The flights list might have multiple flights with the same `DestinationID` (this is not the primary key). - The list should then be sorted (ascending) by the `FinalPrice` property. However, the second sort must not destroy the first one. I mean, a cheaper flight (A) must not be on top of a more expensive flight (B) if `B.DestinationID` appears first than `A.DestinationID` on `totalCityFilters`.