Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created April 18, 2021 09:51
Show Gist options
  • Save SiAust/d288ec33f155ac6ff29c063301bd8d0b to your computer and use it in GitHub Desktop.
Save SiAust/d288ec33f155ac6ff29c063301bd8d0b to your computer and use it in GitHub Desktop.
Sort a list by odd first, then odd asc, even desc
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