3

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;
            }   
        });

5 Answers 5

5

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();
Sign up to request clarification or add additional context in comments.

4 Comments

After you box the stream, you can also call . sort, before converting the stream back. Streams are make for performant chaining of operations
Why can't we pass the primitive type array to sort? or did you meant we can't do this with Comparator argument?
@Ferrybig Thakns! I edited the code. Is this what you're trying to say?
@Mehrose Yes. Comparator and the primitive type array can not be used together.
2

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

2 Comments

Then how does this work? ` int[] arr = {3,4,2,1}; Arrays.sort(arr); ` Here arr is clearly a primitive type array. Thanks!
public static void sort(int[] a) . Arrays class has overloaded sort method to take primite type
2

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
    }   
});

7 Comments

@Mehrose can you explain first what is the purpose of this code?
i have this array mentioned in the code and now I want to sort the numbers in ascending order based on the last(unit digit). Expected output: 21,73,24,45,58,89.
@Mehrose what should happen if two numbers have the same last digit?
the it'll be sort by default nature sorting
Thanks buddy but we don't necessarily type cast the value to Integer as we already defined the Generics with Comparator. The code works fine if I just change the primitive type to Integer object
|
0

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;
    }
}

Comments

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

Comments

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.