fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /* Name of the class has to be "Main" only if the class is public. */
  5. class Ideone {
  6. static int compare(String a, String b) {
  7. int ai = 0, bi = 0;
  8. while (ai < a.length() && bi < b.length()) {
  9. int an = 0;
  10. while (ai < a.length() && a.charAt(ai) != '.') {
  11. an = 10 * an + Character.getNumericValue(a.charAt(ai));
  12. ++ai;
  13. }
  14. ++ai; // Skip the dot.
  15.  
  16. int bn = 0;
  17. while (bi < b.length() && b.charAt(bi) != '.') {
  18. bn = 10 * bn + Character.getNumericValue(b.charAt(bi));
  19. ++bi;
  20. }
  21. ++bi; // Skip the dot.
  22.  
  23. int cmp = Integer.compare(an, bn);
  24. if (cmp != 0) return cmp;
  25. }
  26. if (ai < a.length()) return 1;
  27. if (bi < b.length()) return -1;
  28. return 0;
  29. }
  30.  
  31. public static void main(String[] args) throws java.lang.Exception {
  32. List<String> lines = new ArrayList<>();
  33. try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
  34. String line;
  35. while ((line = reader.readLine()) != null) {
  36. lines.add(line);
  37. }
  38. }
  39. Collections.shuffle(lines);
  40. System.out.println("Shuffled: " + lines);
  41.  
  42. lines.sort(Ideone::compare);
  43. System.out.println("Sorted: " + lines);
  44. }
  45. }
  46.  
Success #stdin #stdout 0.22s 33432KB
stdin
1
1.1
1.1.1
2
2.1
9
9.1
9.9
10
10.1
stdout
Shuffled: [9, 2, 1.1, 1.1.1, 9.1, 9.9, 1, 2.1, 10.1, 10]
Sorted: [1, 1.1, 1.1.1, 2, 2.1, 9, 9.1, 9.9, 10, 10.1]