It’s hard to summarize this as a 1 line question. I have a class like this:
class Item
{
int Count {get; set;}
double Value {get; set;}
}
and a List<Item> that contains an arbitary number of Item values.
How can I get the Item with the lowest Count and the highest Value?
Performance is not important but elegance is, as I don’t want to have huge nested loops to do this operation unless there is not elegant way, i.e. Linq, etc.
EDIT:
Here is a sample list that could have these Items:
{Count, Value}
{2, 10}, {6, 60}, {5, 21}, {4, 65}, {2, 35}, {4, 18}, {3, 55}, {7, 99}, {2, 25}
So here the value I want is {2, 35} because it has the lowest Count of all items, and for the items with the same Count values, it has the highest Value.
Okay, now we’ve got a bit of clarity:
It’s easy…
That will give
nullif the list is empty, but otherwise the item with the lowest count, and the one with the highest value if there are multiple with that count.This is less efficient than it might be, as we could really do it in a single pass by creating a comparer to compare any two values – but this will certainly be the approach which gets the right element in the least code.