Reputation: 59
I'm having a bit of trouble with this code I'm writing. In it, I'm comparing two object elements. I'm trying to use > but I'm finding that it's not working. Is there another way for me to compare them like this without using >? (Also, I'm a beginner so I apologize in advance if the code isn't well done or formatted completely correctly.
private static int indexOfMinInRange(Reservation[] array, int low, int high) {
int index;
Reservation number;
int holdIndex;
number = array[0];
holdIndex =1;
for(index = 0; index < array.length; index++) {
System.out.println(index);
if(number > array[index]) {
number = array[index];
holdIndex = index;
}//end if
}//end for loop
return holdIndex;
}//end indexOfMaxInRange
public static Reservation[] sortArray(Reservation[] arrayGiven) {
int i;
int index;
boolean haveSwapped;
haveSwapped = true;
i = 0;
while(haveSwapped == true) {
haveSwapped = false;
for(i = 0; i + 1 < arrayGiven.length; i++) {
if (arrayGiven[i] > arrayGiven[i + 1]) {
swapElement(arrayGiven, i, i + 1);
haveSwapped = true;
}//end if
}//end for loop (swapping)
}//end while loop
for(index = 0; index < arrayGiven.length; index++) {
System.out.println(arrayGiven[index]);
}//end for loop (printing)
return arrayGiven;
}//end sortArray
Upvotes: 0
Views: 50
Reputation: 1100
If you are not sure the type that each object is,you can use Object#compareTo()
method to compare them.
Each type of the element should implement this method properly.
Upvotes: 2