0

You need to write an array sort. But there is an error "Operator '>' cannot be applied to 'T', 'T'" and "Incompatible types. Found: 'T[]', required: 'T'". What is the problem, why is it?

public class MySortArray <T extends Comparable <T>>{

    public T SortArray(T[] array) {
        int temp;
        for(int i = 0; i < array.length - 1; i++) {
            if(array[i] > array[i+1]) {

            }
        }
        return array;
    }
}
0

1 Answer 1

1

Java does not support operator overloading.

You must use the compareTo() method of Comparable:

if (array[i].compareTo(array[i+1]) > 0)
Sign up to request clarification or add additional context in comments.

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.