I need to sort about 100000 ints with custom ordering rules. If I use Comparator, I need to have an array Integer instances, which is slower to create, slower to sort and takes more memory.
Quick benchmark on Galaxy Nexus with Android 4.2:
createIntArrayAndFillWith1to100000; // 18 ms
createIntegerArrayAndFillWith1to100000; // 191 ms (using Integer.valueOf(i))
Arrays.sort(randomInts); // 354 ms
Arrays.sort(randomIntegers, simpleComparator); // 1734 ms
Is implementing custom sort my best option?
I think the only way is to implement a sort-algorithm on arrays for elementar datatype int yourself. The Java-library does not provide versions for each elementar datatype. boxing/unboxing is the answer.
I would simply implement a sort-algorithm for this special need. (If speed is really such important)
Inplace-sorting with quick-sort is quite easy to implement quicksort (wikipedia)