Reputation: 54
I have a list of classes which house two variables (X and Y). X is the main drive for the sorting and Y acts like a negative multiplier.
The class with the highest X (first) variable and the lowest Y (second) variable needs to be sorted first, while the class with the lowest X and highest Y, as last.
Samples (0-100, 0-10): (100,0) (100, 1) (90, 0) (95, 2) (80, 0) (0,0) (10, 3) (0,5)
Upvotes: 0
Views: 2785
Reputation: 460098
OrderBy where X is highest and Y is least?
So this?
var q = item.OrderByDescending(item => item.x).ThenBy(item => item.y);
If you mean "order by highest x and lowest y" you have to specify the logic.
Maybe you want this logic: you know your maximum values of x and your minimum values of y. Now you want to get the order, so that the items with the nearest x-distance to max-X are first and also those with the lowest distance to min-Y.
Calculate the min- and max-values and then use the distance in this way:
int highestX = items.Max(i => i.X);
int lowestY = items.Min(i => i.Y);
var q = items.OrderBy(item => Math.Min(highestX - item.X, item.Y - lowestY));
That works because i use highestX - item.X
but item.Y - lowestY
.
Upvotes: 4