Search code examples
javasortingwrapperautoboxing

Why I can't use Comparator to sort primitives?


As Java 5 have autoboxing, why I can't use Comparator to sort primitives? An int wouldn't be wrapped into a Integer?


Solution

  • Arrays.sort(..) have dedicated overloadings for sorting primitive arrays.

    If you need any special sorting rules apart from the standard ones, I'm afraid you'd have to use autoboxing. In addition to that, you'd have to transform your array to Integer[], because int[] is not autoboxed.

    And if you are not talking about arrays, but about collections - then you have no choice - collections can hold only objects.