views:

40

answers:

2

Hi folks,

a month ago I asked this question: Trying to find the top 3 properties of a POCO instance. Got an answer, worked well.

Now, I'm trying to find the top 3 properties of a POCO object (like my previous question) but where each property has a WEIGHT. The value of the property comes first. The weight then comes in second ... if two or more properties have the same value.

Lets expand on the data from my previous question as a starting point for an example :-

Lower the weight number the better. 1 == best. 10 is worst. Each weight number is UNIQUE btw. The weight number can be anything ... i just need to somehow weight em....

eg.
Math (weight: 1) - 83%
Engrish (weight: 6) - 82%
Chemistry (weight: 2) - 81%
Drama (Weight: 3) - 100%
Geography (weight: 4) - 82%
Sport (Weight: 5)- 81%
etc..

Top 3 results, in order, are:

  • Drama
  • Math
  • Geography (not Engrish. Geography W = 4, Engrish W = 6)

I'm wondering if this can be achieved with Linq (like my previous question/answer). I'm happy with a dictionary, etc. I'm also guessing I might need to overload the Comparer method?

Cheers for any help :)

A: 

Use the same answer as before, but when ordering multiply the value by the weight.

orip
Multiply the value by the weight? That doesn't sound right.... I'm trying to think that if i have a 100% with a value of 1 and a 99% with a value of 6, then the 99% will be listed first, which isn't right.
Pure.Krome
+1  A: 

Uhm. Based on your previous answer, and made up from me mind. (ie not tested)

var result = (from p in pairs
              orderby p.Value descending, p.Weight
              select p);

var result = pairs.OrderByDescending(p => p.Value)
                  .ThenBy(p => p.Weight);
Simon Svensson
ThenBy .. WTF?! No Way!!!!!! *That* exists?! I'm lovin' linq more and more (if that was possible). brb ....
Pure.Krome
Squirt BANG!!!!!!!!!!!!!!! Winnah. that's so fraking awesome. thanks heaps mate, that made my week! (i love linq and SO).
Pure.Krome