1

I've got this heap sort code, it was originally written to sort an array of integers but I need to sort an Object[] from a class. I keep getting this error for these lines:

public static void heapSort(Object[] myArray, int length) {
      Object temp;
      int size = length-1;
      for (int i = (length / 2); i >= 0; i--) {
         heapify(myArray, i, size);
      };
      for(int i= size; i>=0; i--) {
         temp = myArray[0];
         myArray[0] = myArray[size];
         myArray[size] = temp;
         size--;
         heapify(myArray, 0, size);
      }
   }

public static void heapify (Object [] myArray, int i, int heapSize) {
      int a = 2*i;
      int b = 2*i+1;
      int largestElement;
      if (a<= heapSize && myArray[a] > myArray[i]) {
         largestElement = a;
      } else {
         largestElement = i;
      }
      if (b <= heapSize && myArray[b] > myArray[largestElement]) {
         largestElement = b;
      }
      if (largestElement != i) {
         Object temp = myArray[i];
         myArray[i] = myArray[largestElement];
         myArray[largestElement] = temp;
         heapify(myArray, largestElement, heapSize);
     }
   }

Sort.java:25: error: bad operand types for binary operator '>' if (a<= (heapSize && myArray[a] > myArray[i])) {
first type: Object second type: Object Sort.java:30: error: bad operand types for binary operator '>' if (b <= (heapSize && myArray[b] > myArray[largestElement])) {
first type: Object second type: Object

What does this error mean, I thought it was the bracket placements but it didn't fix the errors.

Is there a better sorting algorithm to sort through an object array created from a class.

2 Answers 2

6

You can't use ">" to compare objects. Try implementing the comparable interface in your object's class so that you can use .compareTo(). If you do this, you could also just use Arrays.sort(yourArray) to sort the array without having to worry about the implementation.

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

Comments

3

Perhaps the easiest way is to define a comparator to handle the actual object you are comparing.

Say you are have an array of SomeClass objects. You can compare them this way.

Object[] objects = {new SomeClass(1), new SomeClass(6), new SomeClass(3),
                new SomeClass(4), new SomeClass(3), new SomeClass(5)};
        
Comparator<Object> comp = (ob1, ob2) -> { 
                   int i1 = ((SomeClass)ob1).id;
                   int i2 = ((SomeClass)ob2).id;
                         return i1 < i2 ? -1 : i1 > i2 ? 1 : 0;};
                             
Arrays.sort(objects, comp);
System.out.println(Arrays.toString(objects));

prints

[1, 3, 3, 4, 5, 6]

The above Comparator works so that the Object array of any type can be used as long as the comparator makes 1) the appropriate casts, and 2) compares the appropriate items of the actual objects under comparison. This would require passing the comparator to the sorting routine (e.g. your heapSort) and use by invoking comp.compare(a,b) where a and b are the array items.

A significantly better way is to use generics and only accept types which implement the Comparable interface as described in thetechnician94's answer. In this case, each class would have the proper logic to compare one or more aspects of the class in a way in which the designer intended. Then all that is necessary is to pass the data structure of objects (in your case, an array) to the sorting method.

class SomeClass {
    public int id;
    public SomeClass(int id) {
        this.id = id;
    }
    public String toString() {
        return id+"";
    }
}

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.