Created
April 18, 2021 09:51
-
-
Save SiAust/d288ec33f155ac6ff29c063301bd8d0b to your computer and use it in GitHub Desktop.
Sort a list by odd first, then odd asc, even desc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<Integer> sortOddEven(List<Integer> numbers) { | |
// sort odd and even | |
// then sort odd asc, even desc | |
Comparator<Integer> byOddFirst = (i1, i2) -> (i1 | 1) == i1 ? -1 : (i2 | 1) > i2 ? 1 : 0; | |
Comparator<Integer> byOddAscEvenDesc = (i1, i2) -> { | |
if ((i1 | 1) == i1 && (i2 | 1) == i2) { | |
return Integer.compare(i1, i2); | |
} else if ((i1 | 1) > i1 && (i2 | 1) > i2) { | |
return Integer.compare(i1, i2) * -1; | |
} else { | |
return 0; | |
} | |
}; | |
numbers.sort(byOddFirst); | |
numbers.sort(byOddAscEvenDesc); | |
return numbers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment