4

I'm a new programmer to java, been using this website a lot to learn the ways. Today I've stumbled into another problem towards a program I'm doing for practice.

I have an array:

    final int EXAMS = 5;
    int[] scores = new int [EXAMS];

The values for this array are then asked from the user through a Scanner object:

for (int index = 0; index < EXAMS; index++)
    {
        System.out.println("Enter the score for " + (index+1) +":");
        scores[index] = kb.nextInt();
        if (scores[index] < 0){
            System.out.println("The number you have entered is invalid.");
            scores[index] = kb.nextInt();
        }
    }

I managed to make an ascending order sorting of the values "scores[]":

       Arrays.sort(scores);
        System.out.println("The sorted int array is:");
        for (int number : scores) 
        {
            System.out.println("Number = "+ number);
        }

But I want the sorting to be in descending order. When I put the

Arrays.sort(scores, Collections.reverseOrder());

I get an error saying: "no suitable method found for sorting." Please help.

1
  • Sort the Array at the time of insertion.. Commented Aug 10, 2015 at 16:41

7 Answers 7

3
Collections.reverseOrder() 

Won't work because the array is of primitive type. You can change from int to Integer or try something different like:

Arrays.sort(scores);
ArrayUtils.reverse(scores);

Your final option would be to make your own code.

Sign up to request clarification or add additional context in comments.

Comments

3

The following works (using the Integer Objects instead of primitive int):

Integer[] test = {1, 2, 4, 3, 5};
Arrays.sort(test, Collections.reverseOrder());
System.out.println(Arrays.toString(test)); // [5, 4, 3, 2, 1]

Comments

2

There's no sort method in Arrays class that accepts an int[] and a Comparator. The sort methods in the Arrays class that accept a Comparator require an array of reference type (while an int[] is an array of a primitive type).

If you change the type of your scores array from int[] to Integer[], your code will work, since the method public static <T> void sort(T[] a, Comparator<? super T> c) of the Arrays class will match your Arrays.sort(scores, Collections.reverseOrder()); call.

final int EXAMS = 5;
Integer[] scores = new Integer [EXAMS]; // the only required change
for (int index = 0; index < EXAMS; index++) {
    System.out.println("Enter the score for " + (index+1) +":");
    scores[index] = kb.nextInt();
    if (scores[index] < 0){
        System.out.println("The number you have entered is invalid.");
        scores[index] = kb.nextInt();
    }
}
Arrays.sort(scores, Collections.reverseOrder());
System.out.println("The sorted int array is:");
for (int number : scores)  {
    System.out.println("Number = "+ number);
}

Comments

1

You just need to change your array type from Primitive type to Primitive wrapper class i.e. int[] scores = new int [EXAMS] to Integer[] scores = new Integer[EXAMS]

Arrays.sort accept wrapper array in the first argument not primitive.

Comments

0
Arrays.sort(a, Collections.reverseOrder());

does not work with primitives. Try this,

Integer[] a = new Integer[]{1,2,3,5,4};
            Arrays.sort(a,Collections.reverseOrder());
            for(int i:a)
            {
                System.out.println(i);
            }

Comments

0

There is no sort method for arrays in java.But ArrayList have a sort method.You can the below code to sort the list in reverse order.

List l=new ArrayList();
l.add(5);
l.add(1);
l.add(2);
l.add(3);
Collections.sort(l,Collections.reverseOrder());
System.out.println(l);

Comments

0

i would initialise the values first.

// we do 1 to 6 because, 6th iteration is where we going to stop
// so that it will count up to 5.
int[] arr = IntStream.range(1, 6).toArray();

then the value of it u pass in the array below.

if we need to sort with primitives int, this would be the approach i would take.

  1. sort the list in ascending order
  2. reverse all the values

NOTE: this approach would be an N log N time complexity. of course it will be able to sort faster at N time complexity but this code is most readable and easy to understand for me.

// this func sorts in n log n time complexity
    public void sortReverse(int[] arr) {
        // 1. now sort all values in descending order
        for (int i = 0, j = arr.length - 1; i < arr.length / 2;i++) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            j--;
        }
    }

1 Comment

i have updated the function to be in camelcase according to java naming convention ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.