Problem with sorting
I am having a problem with a sorting method. I am trying to sort through an array. When I compile I get a message stating
operator > cannot be applied to Job, Job
I get the message from the if statement.
Can someone tell me what I'm doing wrong?
the method is below
/**
* Sorts the Job array.
*
*/
publicvoid sort(Job[] arr,int left,int right)
{
for (int i = left; i < right; ++i)
{
int min = i;
for (int j = i; j < right; ++j)
{
if (arr[min] > arr[j])
{
min = j;
}
}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
[1303 byte] By [
sgt_spikea] at [2007-10-2 17:09:53]

The compiler message is pretty clear. You can't use < or > to compare objects like that. How is one object more or less than another?
If only there was a way to compare objects of similar type. We could have a method and call it compareTo and make it part of an interface we could call... oh I don't know... Comparable. That would be sweet.
Read this http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
I get what you are saying. I have a compareTo method. How does that tie into the sort? What I have is an array of objects that have three type parameters(int, String, double). How do I reference the int of the array. I need to sort the array by this int
Whole lot of sorting going on
public class Job implements Comparable{
int someValue;
// constructors and other methods here
public int compareTo(Object o)throws ClassCastException{
/* compare the someValue with this with o (which needs to be a Job)
return -1, 0 or 1 if THIS Job is less than, equal to or more than
the job o?*/
}
}
Arrays.sort(jobArray);