1

For the current implementation I'm using an array of integers, Integer[], to use an external comparator. The order is not the 'natural' one but one defined externally.

Arrays.parallelSort(Integer[] array, Comparator<T> cmp);

Is there a way using parallelSort with an array of integers, int[], and a user defined comparator ?

instead , something like :

Arrays.parallelSort(int[] array, IntComparator cmp);

Performance is the issue (think if you've 10mio ints)

ic3
  • 7,917
  • 14
  • 67
  • 115

1 Answers1

1

If you want to use a custom comparator with the standard Java SE Arrays.parallelSort(...) methods on an array of integers, then it has to be an Integer[] rather than an int[].

Here is an example (untested / based on https://stackoverflow.com/a/33045466/139985)

int[] ia = {99, 11, 7, 21, 4, 2};
ia = Arrays.stream(ia).
    boxed().
    parallel().
    sorted((a, b) -> b.compareTo(a)). // sort descending
    mapToInt(i -> i).
    toArray();

that converts from int[] to Integer and back using streams.

There are various other alternatives using 3rd party libraries:

Unfortunately, none of the answers to the above use a parallel sort, but there may be alternatives that do.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • That's somehow what we're doing :-) – ic3 Aug 03 '18 at 11:20
  • Well ... there ain't any alternative unless you want to look for an obscure 3rd-party library that does parallel sorting of `int[]` with a custom "comparator". – Stephen C Aug 03 '18 at 11:26