Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajnenie;
- import java.util.Arrays;
- import java.util.Scanner;
- public class CustomComparator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Integer[] numbers = Arrays.stream(input.split("\\s+"))
- .map(Integer::parseInt)
- .toArray(Integer[]::new);
- Arrays.sort(numbers, (a, b) -> {
- if (a % 2 == 0 && b % 2 != 0) {
- return -1; // Even numbers come before odd numbers
- } else if (a % 2 != 0 && b % 2 == 0) {
- return 1; // Odd numbers come after even numbers
- } else {
- return a.compareTo(b); // Both even or both odd, sort in ascending order
- }
- });
- for (int num : numbers) {
- System.out.print(num + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement