-2

my Arrays.sort() command using comparator is not working. I want to sort the array in descending order. My program is as follows:

import java.util.*;
import java.io.*;

class MinScalProd
{
public static void main(String[] ar) throws IOException
{
Scanner input=new Scanner(new File("Min.in"));
FileWriter fw = new FileWriter("Min.out");
int T=input.nextInt();
    for(int cases = 1; cases <= T; cases++)
    {
        int n=input.nextInt();
        int[] v1=new int[n];
        int[] v2=new int[n];
        int sp=0;
        for(int i=0;i<n;i++)


        {
            v1[i]=input.nextInt();
        }
        for(int i=0;i<n;i++)
        {
            v2[i]=input.nextInt();
        }
        Comparator comp = Collections.reverseOrder();
        Arrays.sort(v1);
        Arrays.sort(v2,comp);/*here it is giving error(Cannot find symbol)*/
        for(int i=0;i<n;i++)
        {
        sp=sp+v1[i]*v2[i];
        }
        fw.write("Case #" + cases + ": "+sp+"\n");


    }
fw.flush();
fw.close();
}
}

I hope that you guys will solve it real quick

1 Answers1

0

sort(T[] a, Comparator<? super T> c) only accepts arrays of reference types, it doesn't work for primitive arrays. You can change v2 to be an array of Integer.

Change

int[] v2=new int[n];

to

Integer[] v2=new Integer[n];
Eran
  • 387,369
  • 54
  • 702
  • 768