Mehrose
Mehrose

Reputation: 87

Arrays.sort() is not accepting the comparator type object

Am I missing something? It gives an error on sort method (doesn't identify the comparator method) but if I use Integer type array then this error goes away. Can't we do this comparison with primitive type of arrays?

public class Test {
    public static void main(String[] args) {
        int a[]={21,58,89,45,73,24};
        Arrays.sort(a,new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                if(o1%10>o2%10) return -1;
                return 1;
            }   
        });

Upvotes: 3

Views: 4819

Answers (5)

Faseela Thayattuchira
Faseela Thayattuchira

Reputation: 527

 Arrays.sort(array, Comparator.comparingInt(interval -> interval[0]));

Upvotes: 0

Misha Betekhtin
Misha Betekhtin

Reputation: 126

You can perform sorting with Comparator on Integer because Integer is implementing Comparable Interface. primitive can be sorted only by the native way - int for example by number order.

You can do one of the following:

  1. Convert int to Integer int a[]={21,58,89,45,73,24}; to Integer a[]={21,58,89,45,73,24};
  2. if using int don't use Comparator Arrays.sort(a);
  3. create your class, implement Comparable Interface and then manipulate int array inside - Harder approach and not recommended
public class MyInt implements Comparable<Integer> {

    private final int value;

    public MyInt(int value) {
        this.value = value;
    }

    @Override
    public int compareTo(Integer o) {
        return compare(this.value, o);
    }

    public static int compare(Integer o1, Integer o2) {
        if(o1%10>o2%10) return -1;
        return 1;
    }
}

Upvotes: 0

Madplay
Madplay

Reputation: 1037

You can't pass a primitive type to sort method. Can you try this?

// Your array
int a[]={21, 58, 89, 45, 73, 24};

int[] ints = Arrays.stream(a).boxed().sorted(new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        if (o1 % 10 > o2 % 10) return -1;
        return 1;
    }
}).mapToInt(Integer::intValue).toArray();

Upvotes: 4

Eran
Eran

Reputation: 393781

No you cannot. Primitive arrays can only be sorted with the methods that accept primitive arrays, such as static void sort(int[] a).

You can't pass a primitive array to public static <T> void sort(T[] a, Comparator<? super T> c), since the generic type parameter can only be substituted with a reference type, not a primitive type.

Besides, your Comparator makes no sense and violates the general contract of the Comparator interface. Therefore you have no reason to use that Comparator (not even with Integer arrays).

Based on your comments, you need something like this:

Integer a[]={21,58,89,45,73,24};
Arrays.sort(a,new Comparator<Integer>(){
    @Override
    public int compare(Integer o1, Integer o2) {
        int l1 = o1 % 10;
        int l2 = o2 % 10;
        if (l1 != l2)
            return Integer.compare(l1,l2); // sort by last digit
        else
            return Integer.compare(o1,o2); // sort by natural order
    }   
});

Upvotes: 2

Rafał Sokalski
Rafał Sokalski

Reputation: 2007

Signature of method is: public static <T> void sort(T[] a, Comparator<? super T> c) so the first argument is generic and you cannot put there primitive type that's why you get an error. For such sorting you have predefined sort method which can take primitive arrays

Upvotes: 2

Related Questions