I am trying to sort an array of structs by he strings they hold. i contain their strings and then compare them to see if they should be swapped. then i swap them if they should. Though when i sort them they are out of order. below is my code and sample output... how can this be fixed;
Sample output:
cloth
life
plate
knife
rock
for (int i = 0; i < count; i++)
{
string temp = Arr[i].name;
for (int j = 0; j < count; j++)
{
string temp2 = Arr[j].name;
if (i != j && temp[0] > temp2[0])
{
swap(Arr[j], Arr[i]);
}
else if (i != j )
{
int k = 0;
int smallest;
if (temp.size() < temp2.size())
{
smallest = temp.size();
}
else
{
smallest = temp2.size();
}
while (i != j && temp[k] < temp2[k] && k != smallest)
{
k++;
if (temp[k] < temp2[k])
{
swap(Arr[j], Arr[i]);
}
}
}
}
}
Sample output:
cloth
life
plate
knife
rock