fork download
  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3. import java.util.Scanner;
  4.  
  5. public class Main{
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int n = scanner.nextInt();
  10. String[] numbers = new String[n];
  11.  
  12. for (int i = 0; i < n; i++) {
  13. numbers[i] = String.valueOf(scanner.nextInt());
  14. }
  15.  
  16. // Custom comparator for sorting
  17. Comparator<String> comparator = new Comparator<String>() {
  18. @Override
  19. public int compare(String x, String y) {
  20. // Compare concatenated results
  21. return (x + y).compareTo(y + x);
  22. }
  23. };
  24.  
  25. // Sort using the custom comparator
  26. Arrays.sort(numbers, comparator);
  27.  
  28. // Output the result
  29. System.out.println(String.join(" ", numbers));
  30. }
  31. }
  32.  
Success #stdin #stdout 0.17s 59140KB
stdin
3
2 1 21
stdout
1 21 2