Sorting Class String Array

Hey everyone,
Im working on this project and im having trouble sorting an array and I cant figure out what I did wrong.

grocery is the class in this project. Everything seems to sort alphabetically and correctly except the original 0 element of the array which is All-Natural Yogurt. For some reason it is placed in the 11th element even though it begins with an A and should be in the 1 element. Im assuming something is wrong with my sort function. Below is the list of names im sorting, and my sort function. The .getName() returns the name of an item. Thanks for any help.

All-Natural Yogurt
Oat Cereal
Golden Tequila(1.75L)
Full Body Shampoo
First Aid Ointment
Alkaline Batteries
Ibuprofen
Cherry Soda
Draft Beet 12-pack
Laundry Detergent
Apples(3 lb bag)
Notebook Paper

void Sort(grocery array[], int SIZE)
{

int swp_index = 0; //Smallest value
grocery temp;


for (int a = 0; a < (SIZE - 1); a++)
{
swp_index = a;
for(int index = a + 1; index < SIZE; index++)
{
if (array[index].getName() < array[swp_index].getName() )
{
swp_index = index;
}
}

temp = grocery[a];
array[a] = array[swp_index];
array[swp_index] = temp;

}
}
Last edited on
Forgot to mention. This is an object array i am trying to sort I do not know if that will make a difference. Thanks.
I think you should begin your main loop from 1 not from 0

for (int a = 0; a < (SIZE - 1); a++)
Topic archived. No new replies allowed.