2

So I have this sorting function that is suppose to take in an array of structs and I need to organize them by name, both have a first and last name and if their last names are the same then I must move on to the first name to compare as well. So I made 2 strings that contain the last and first name combined into 1, and I iterate through the list and see which is small and move it up. But the issue is...it doesnt do anything...at all, and I don't understand why!?

void sortStruct(struct student *list, int studentCount){

int j, k;

struct student temp;

char buffer[35];
char buffer2[35];

for (j = 0 ; j <= studentCount-2 ; j++){

    sprintf(buffer, "%s, %s", list[j].lastname, list[j].firstname);

    for(k = 1 ; k <= studentCount-1 ; k++){

        sprintf(buffer2, "%s, %s", list[k].lastname, list[k].firstname);

        if(buffer < buffer2){

            temp = list[j];
            list[j] = list[k];
            list[j] = temp;

        }
    }
}
}

Anyone know whats wrong?

7
  • 4
    buffer < buffer2 is absolutely not what you want. That's just comparing two memory addresses! You should use the function strcmp(). Commented Mar 3, 2013 at 4:52
  • Add it as answer Apprentice Queue :) Commented Mar 3, 2013 at 4:53
  • I thought that compares the length of the strings!? Commented Mar 3, 2013 at 4:54
  • @Sherifftwinkie No, it doesn't. If you want to sort by length then use strlen(). i.e. if(strlen(buffer) <strlen(buffer2)) Commented Mar 3, 2013 at 4:57
  • Do you want to sort based on length comparison or string comparison? Commented Mar 3, 2013 at 4:58

2 Answers 2

4

buffer < buffer2 is absolutely not what you want. That's just comparing two memory addresses! You should use the function strcmp(). For example,

if (strcmp(buffer, buffer2) < 0)

Sign up to request clarification or add additional context in comments.

Comments

2

Your function should be like below:

For Name Comparison

void sortStruct(struct student *list, int studentCount)
{

   int j, k;
   struct student temp;
   char buffer[35];
   char buffer2[35];

   for (j = 0 ; j <= studentCount-2 ; j++)
   {
     sprintf(buffer, "%s, %s", list[j].lastname, list[j].firstname);
     for(k = 1 ; k <= studentCount-1 ; k++)
     {
       sprintf(buffer2, "%s, %s", list[k].lastname, list[k].firstname);
       if(strcmp(buffer, buffer2)<0)
       {
            temp = list[j];
            list[j] = list[k];
            list[j] = temp;
       }
     }
   }
}

For Name Length Comparison

void sortStruct(struct student *list, int studentCount)
{

  int j, k;
  struct student temp;
  char buffer[35];
  char buffer2[35];

  for (j = 0 ; j <= studentCount-2 ; j++)
  {
    sprintf(buffer, "%s, %s", list[j].lastname, list[j].firstname);

    for(k = 1 ; k <= studentCount-1 ; k++)
    {

     sprintf(buffer2, "%s, %s", list[k].lastname, list[k].firstname);

      if(strlen(buffer)< strlen(buffer2))
      {
         temp = list[j];
         list[j] = list[k];
         list[j] = temp;
      }
    }
 }
}

1 Comment

Is the content of the if proper? It still doesn't change anything I dont see what is wrong with what I tell it to do upon filling that condition?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.