fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.*;
  7.  
  8. @SuppressWarnings("unchecked")
  9. class NumberComparator implements Comparator<Number> {
  10.  
  11. // Special values that are treated as larger than any other.
  12. private final static List<?> special =
  13. Arrays.asList(Double.NaN, Float.NaN, null);
  14.  
  15. private final static List<?> largest =
  16. Arrays.asList(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
  17.  
  18. private final static List<?> smallest =
  19. Arrays.asList(Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
  20.  
  21. public int compare(Number n1, Number n2) {
  22.  
  23. // Handle special cases (including null)
  24. if (special.contains(n1)) return 1;
  25. if (special.contains(n2)) return -1;
  26.  
  27. if (largest.contains(n1) || smallest.contains(n2)) return 1;
  28. if (largest.contains(n2) || smallest.contains(n1)) return -1;
  29.  
  30. // Promote known values (Byte, Integer, Long, Float, Double and
  31. // BigInteger) to BigDecimal, as this is the most generic known type.
  32. BigDecimal bd1 = asBigDecimal(n1);
  33. BigDecimal bd2 = asBigDecimal(n2);
  34. if (bd1 != null && bd2 != null)
  35. return bd1.compareTo(bd2);
  36.  
  37. // Handle arbitrary Number-comparisons if o1 and o2 are of same class
  38. // and implements Comparable.
  39. if (n1 instanceof Comparable<?> && n2 instanceof Comparable<?>)
  40. try {
  41. return ((Comparable) n1).compareTo((Comparable) n2);
  42. } catch (ClassCastException cce) {
  43. }
  44.  
  45. // If the longValue()s differ between the two numbers, trust these.
  46. int longCmp = ((Long) n1.longValue()).compareTo(n2.longValue());
  47. if (longCmp != 0)
  48. return longCmp;
  49.  
  50. // Pray to god that the doubleValue()s differ between the two numbers.
  51. int doubleCmp = ((Double) n1.doubleValue()).compareTo(n2.doubleValue());
  52. if (doubleCmp != 0)
  53. return longCmp;
  54.  
  55. // Die a painful death...
  56. "Cannot compare " + n1 + " with " + n2);
  57. }
  58.  
  59.  
  60. // Convert known Numbers to BigDecimal, and the argument n otherwise.
  61. private BigDecimal asBigDecimal(Number n) {
  62. if (n instanceof Byte) return new BigDecimal((Byte) n);
  63. if (n instanceof Integer) return new BigDecimal((Integer) n);
  64. if (n instanceof Short) return new BigDecimal((Short) n);
  65. if (n instanceof Long) return new BigDecimal((Long) n);
  66. if (n instanceof Float) return new BigDecimal((Float) n);
  67. if (n instanceof Double) return new BigDecimal((Double) n);
  68. if (n instanceof BigInteger) return new BigDecimal((BigInteger) n);
  69. if (n instanceof BigDecimal) return (BigDecimal) n;
  70. return null;
  71. }
  72. }
  73.  
  74.  
  75.  
  76. /* Name of the class has to be "Main" only if the class is public. */
  77. class Ideone
  78. {
  79. public static void main(String[] args) {
  80. List<Number> li = new ArrayList<Number>();
  81.  
  82. // Add an Integer, a Double, a Float, a Short, a Byte and a Long.
  83. li.add(20); li.add((short) 17);
  84. li.add(12.2); li.add((byte) 100);
  85. li.add(0.2f); li.add(19518926L);
  86. li.add(Double.NaN); li.add(Double.NEGATIVE_INFINITY);
  87. li.add(Float.NaN); li.add(Double.POSITIVE_INFINITY);
  88.  
  89. // A custom Number
  90. li.add(new BoolNumber(1));
  91. li.add(new BoolNumber(0));
  92.  
  93. // Add two BigDecimal that are larger than Double.MAX_VALUE.
  94. BigDecimal largeDec = new BigDecimal("" + Double.MAX_VALUE);
  95. li.add(largeDec/*.multiply(BigDecimal.TEN)*/);
  96. li.add(largeDec.multiply(BigDecimal.TEN).multiply(BigDecimal.TEN));
  97.  
  98. // Add two BigInteger that are larger than Double.MAX_VALUE.
  99. BigInteger largeInt = largeDec.toBigInteger().add(BigInteger.ONE);
  100. li.add(largeInt.multiply(BigInteger.TEN));
  101. li.add(largeInt.multiply(BigInteger.TEN).multiply(BigInteger.TEN));
  102.  
  103. // ...and just for fun...
  104. li.add(null);
  105.  
  106. Collections.shuffle(li);
  107. Collections.sort(li, new NumberComparator());
  108.  
  109. for (Number num : li)
  110. System.out.println(num);
  111. }
  112.  
  113. static class BoolNumber extends Number {
  114. boolean b;
  115. public BoolNumber(int i) { b = i != 0; }
  116. public double doubleValue() { return b ? 1d : 0d; }
  117. public float floatValue() { return b ? 1f : 0f; }
  118. public int intValue() { return b ? 1 : 0; }
  119. public long longValue() { return b ? 1L : 0L; }
  120. public String toString() { return b ? "1" : "0"; }
  121. }
  122. }
Success #stdin #stdout 0.12s 320320KB
stdin
Standard input is empty
stdout
-Infinity
0.2
0
1
12.2
17
20
100
19518926
1.7976931348623157E+308
1797693134862315700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010
1.797693134862315700E+310
17976931348623157000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100
Infinity
NaN
NaN
null