fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9. public static void main(String[] args) {
  10. Integer[] arr = {1, 2, 3, 4, 5, 6, 7};
  11.  
  12. Arrays.sort(arr, new Comparator<Integer>() {
  13. @Override
  14. public int compare(Integer o1, Integer o2) {
  15. if (o1 % 2 == 0) {
  16. if (o1 - 1 == o2 || o1 + 1 == o2) {
  17. return -1;
  18. }
  19. }
  20.  
  21. return Integer.compare(o1, o2);
  22. }
  23. });
  24.  
  25. System.out.println(Arrays.toString(arr));
  26. }
  27. }
Success #stdin #stdout 0.09s 27744KB
stdin
Standard input is empty
stdout
[2, 1, 4, 3, 6, 5, 7]