0

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)

  • 1
    It would be awesome if you could provide a [mcve] with at least 6 sample inputs and expected results based on those sample inputs. _This is important since I could interpret your question in many different ways._ – mjwills Jun 15 '18 at 13:33
  • What means by both? You want to add the values? `OrderBy(item => item.x + item.y)` – Tim Schmelter Jun 15 '18 at 13:34
  • I think you are looking for .ThenBy , but without any code, it's hard to guess ! Post your code here – Kaj Jun 15 '18 at 13:34
  • Possible duplicate of [Multiple "order by" in LINQ](https://stackoverflow.com/questions/298725/multiple-order-by-in-linq) – Equalsk Jun 15 '18 at 13:37
  • You should provide an short list of example (x,y) pairs and show the order that you would like them in. For example: Descending by X then Ascending by Y would be (3,1) (3,2) (3,3) (2,1) (2,2) (2,3) (1,1) (1,2) (1,3) – Wyck Jun 15 '18 at 13:44
  • If you mean _"order by high x or low y"_ then you have to specify the logic. Is x=5 "higher" than the low value y=4? – Tim Schmelter Jun 15 '18 at 14:03
  • I tried to re-phrase the question, I hope it helps. – Patrick Scheper Jun 17 '18 at 14:02
  • @PatrickScheper - It's confusing the hell out of me. The previous version seemed more sensible. – Enigmativity Jun 17 '18 at 14:16
  • @Enigmativity I am sorry, the second answer Tim gave, did the trick. Thanks anyway. – Patrick Scheper Jun 17 '18 at 14:18
  • 1
    @PatrickScheper - No worries, but you should still try to fix the question. The primary role of SO is to create a library of questions and answers for future readers. It's only secondary that the person who asks a question gets the answer. It'd be great if you could still clarify the question. – Enigmativity Jun 17 '18 at 22:51

1 Answers1

4

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.

Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • Are you sure about using OrderByDescending for x value ? Since I understood he wants the highest x value first !! – Kaj Jun 15 '18 at 13:49
  • 1
    @Kaj: well, isn't it that? This will return the highest x values and only in case of ties the y value is taken into account. – Tim Schmelter Jun 15 '18 at 13:51
  • well, the result for the x value would start from 1 to 10 ! I think he would order them from 10 to 1 , Am I missing something here ? – Kaj Jun 15 '18 at 13:52
  • @Kaj: nope, it will start with 10 because of `OrderByDescending` as opposed to `OrderBy` – Tim Schmelter Jun 15 '18 at 13:53
  • My fault :) I thought it's written ascending ! Sorry for that :) – Kaj Jun 15 '18 at 13:55
  • Yeah, I actually wanted something like the second answer and it totally works. Thank you so much @TimSchmelter! – Patrick Scheper Jun 17 '18 at 14:14