Ayk96
Ayk96

Reputation: 79

Sort array middle to low to high

I have a list of points which I first sort ascending on the x value. Now I'd like to sort the list again based on the y-value but from middle to low to high (or middle to high to low, doesn't really matter).

This is my java code:

Collections.sort(list, Comparator.comparing((Integer[] p) -> p[0]).thenComparing(**"middle to low to high"**));

For example:

Initial list: {(10,9),(11,8),(10,5),(10,7),(8,9),(11,9)}

First sorting: {(8,9),(10,9),(10,5),(10,7),(11,8),(11,9)}

Second sorting: {(8,9),(10,7),(10,5),(10,9),(11,8),(11,9)} this is where I need help

Is there a way to write a comparator for this?

Thank you!

Upvotes: 2

Views: 391

Answers (1)

Martin G
Martin G

Reputation: 329

Rather than trying a new Comparator, change the numbers. Say your interval is (0..12), middle is 7, then the easiest way is to change all values >7 to v-13, sort the result and change back by adding 13 to all negative values.

Upvotes: 1

Related Questions