Re: Reverse sorting an array
On 9/30/2011 4:27 PM, Fred wrote:
But I must be missing something obvious here - why does this print
zeroes?
....
printArray(positives, positivesIndex);
Arrays.sort(positives);
printArray(positives, positivesIndex);
....
Arrays.sort without a specified fromIndex and toIndex sorts the entire
array. I would expect to see (positives.length - positivesIndex) zero
elements before the first positive element in the sorted array.
Given the facts that you are reading a variable number of elements and
you need to do reverse sorting, are you *sure* you should be working
with int rather than Integer? If you used Integer, you could build up a
List exactly the right length, avoiding any extra zero elements. You
could use Collections.sort with the Collections.reverseOrder()
comparator to directly sort them in descending order. The whole thing
would be simpler.
At least so far, you are not doing any arithmetic that would need int,
and you don't have anywhere near enough elements for there to be a
significant performance difference.
Patricia