fork download
  1. import java.util.Arrays;
  2.  
  3. class Match implements Comparable {
  4. public int matches = 0;
  5. public double distance = 0.0;
  6. public int compareTo(Object o) {
  7. Match m = (Match)o;
  8. int diff = m.matches - matches;
  9. if (diff == 0) {
  10. if (distance > m.distance) {
  11. return 1;
  12. } else if (distance < m.distance) {
  13. return -1;
  14. } else {
  15. return 0;
  16. }
  17. } else {
  18. return diff;
  19. }
  20. }
  21. }
  22.  
  23. class Main {
  24. public static void main(String[] args) {
  25. Match m = new Match();
  26. Match[] matches = new Match[50];
  27. for (int i = 0; i < matches.length; i++) {
  28. matches[i] = new Match();
  29. matches[i].matches = i % 6 - 3;
  30. matches[i].distance = Math.random() * 2 - 1;
  31. }
  32. Arrays.sort(matches);
  33. for (int i = 0; i < matches.length; i++) {
  34. System.out.printf("%6d%16.8f\n", matches[i].matches, matches[i].distance);
  35. }
  36. }
  37. }
  38.  
Success #stdin #stdout 0.09s 215616KB
stdin
Standard input is empty
stdout
     2     -0.41208181
     2     -0.27590751
     2     -0.18748857
     2      0.15462147
     2      0.17511263
     2      0.69097020
     2      0.86596439
     2      0.90277683
     1     -0.94534963
     1     -0.88226147
     1     -0.65329436
     1     -0.64345658
     1     -0.16213242
     1     -0.03848919
     1      0.66350067
     1      0.89725433
     0     -0.86355951
     0     -0.76057038
     0     -0.50115000
     0     -0.28123029
     0     -0.19103748
     0      0.57581275
     0      0.66587328
     0      0.81719011
    -1     -0.90787705
    -1     -0.63766475
    -1     -0.58883338
    -1     -0.27360423
    -1      0.25631227
    -1      0.35079107
    -1      0.49498049
    -1      0.88538185
    -2     -0.84065919
    -2     -0.77938111
    -2     -0.77523495
    -2     -0.50957570
    -2     -0.46264759
    -2     -0.44943689
    -2      0.07668930
    -2      0.61136088
    -2      0.79774804
    -3     -0.99899085
    -3     -0.53535864
    -3     -0.35825968
    -3     -0.26665261
    -3     -0.15804698
    -3     -0.01315807
    -3     -0.00097662
    -3      0.52294226
    -3      0.57105836