I'm trying to do selection sort for objects...But it wont work, please help. We were told to change the first one to do objects. Any help appreciated

public static void selectionSort (int[] numbers)
   {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < numbers.length; scan++)
            if (numbers[scan] < numbers[min])
               min = scan;

         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
      }
   }

   public static void selectionSort (Comparable[] objects)
   {
      int min;
      	Comparable temp;

      for (int index = 0; index < objects.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < objects.length; scan++)
            if (objects[min].compareTo(objects[scan]))
               min = scan;

         // Swap the values
         temp = objects[min];
         objects[min] = objects[index];
         objects[index] = temp;
      }
   }

for starters:
what are you testing for here:

if (objects[min].compareTo(objects[scan]))

also, you're shifting the value of 'min' quite some times. didn't really fully read the code, but are you sure that doesn't cause any problems?

I think, though I am not 100% sure, that the problem is rooted in the if (objects[min].compareTo(objects[scan])) statement over here. compareTo() returns either 1,0,-1 depending on whether the calling object is greater than, equal to or less than the object passed as a parameter.
According to the above statement you seem to be of the notion here that if(1) would make the control move into the if condition. But I don't think if(1) would be good enough, according to The Java Language Specification the if() structure requires the expression to result in a boolean type, else it gives a compile-time error. Is your program compiling correctly? even if it is I suggest you get the if(1) structure confirmed.

To elaborate on my previous post further, if the result of the expression in the if stucture is of the type Boolean it is converted to boolean by using the method booleanValue of the Boolean class. These conversions are called Unboxing Conversions You can read more about them here

Having said all this, if the point mentioned by me is not what is causing the problem in your case then I suggest, you let us know (and know yourself, very clearly) what is that that you are comparing. There are certain things that you as the implementor need to be careful while using compareTo . These things are mentioned in the javadocs for compareTo

PLease help, here are the files. They already gave the object one with insertion sort, but I can't do selection

Did you happen to read the above posts ? Did you try out the things mentioned there ? Did you go through the links ? Those may help you in eradicating your problem. Have you really 'tried' to help yourself ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.