sorting an array of strings

This is a discussion on sorting an array of strings within the C Programming forums, part of the General Programming Boards category; i'm trying to sort an array of strings which I have declared globally. Each string is a number following by ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    20

    sorting an array of strings

    i'm trying to sort an array of strings which I have declared globally. Each string is a number following by a word (e.g. 14 socks). I'm just interested in sorting by number as of now.

    I've tried two different sorting techniques, and both compile and run but nothing gets sorted. Here's my code for one technique (the other technique is a selection sort):

    Code:
    char global[500][15];
    char buff1[128]; //local

    Code:
     for (i=0;i<place;i++)
          for (j=0;j<place;j++) //place is the number of elements in the array of strings
             {
                 if(strcmp(global[i], global[j]) > 0) {
                    strcpy(buff1, &global[i][12]);
                    strcpy(&global[i][12], &global[j][12]);
                    strcpy(&global[j][12], buff1);
                 }
             }
    Is there something I'm missing?

    I also know I can use qsort, but since I'm unfamiliar with that I'd prefer something that involves using a swap, like what I have above.
    Last edited by porstart; 02-22-2011 at 06:41 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by porstart View Post
    i'm trying to sort an array of strings which I have declared globally. Each string is a number following by a word (e.g. 14 socks). I'm just interested in sorting by number as of now.

    I've tried two different sorting techniques, and both compile and run but nothing gets sorted. Here's my code for one technique (the other technique is a selection sort):

    Code:
    char global[500][15];
    char buff1[128]; //local

    Code:
     for (i=0;i<place;i++)
          for (j=0;j<place;j++) //place is the number of elements in the array of strings
             {
                 if(strcmp(global[i], global[j]) > 0) {
                    strcpy(buff1, &global[i][12]);
                    strcpy(&global[i][12], &global[j][12]);
                    strcpy(&global[j][12], buff1);
                 }
             }
    Is there something I'm missing?

    I also know I can use qsort, but since I'm unfamiliar with that I'd prefer something that involves using a swap, like what I have above.
    The most common problem is caused by the "invisible" char's: '\n' and '\0', being the newline and end of string char, respectively.

    You WANT the '\0' there, or else you just have a bunch of char's, and no string is in sight. You do NOT want the newline. So before any sorting, let's remove the newline:
    Code:
    len = strlen(Global[i];    //include <string.h> if you don't have it already included
    if(Global[len-1] == '\n')
      Global[len-1] = '\0';  //overwriting and shortening the string one char's worth
    Note that strcmp is not sorting "by numbers". It sorts only according to the entire string (which here, should be what you want).

    Then let's give your substitution sort, a bit more zip, and get rid of these 12's:
    (Why the 12's??).

    Code:
     for (i=0;i<place-1;i++)
          for (j=i+1;j<place;j++) //place is the number of elements in the array of strings
             {
                 if(strcmp(global[i], global[j]) > 0) {
                    strcpy(buff1, global[i]);
                    strcpy(global[i], global[j]);
                    strcpy(global[j], buff1);
                 }
             }
    Global[i] is the address of the string at Global[i], so no & (ampersand), is needed for it. In C, 2D arrays are simply "arrays of arrays".

    Try that. Please copy and paste the entire code above, because the innards of the sort, have been changed, as well.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    20
    Very helpful, thanks.


    Code:
    len = strlen(Global[i];    //include <string.h> if you don't have it already included
    if(Global[len-1] == '\n')
      Global[len-1] = '\0';  //overwriting and shortening the string one char's worth

    For this, I'm getting a warning for the if statement and an error for the following line (I assumed len should be an int):
    warning: comparison between pointer and integer
    error: incompatible types when assigning to type 'char[15]' from type 'int'


    Code:
     for (i=0;i<place-1;i++)
          for (j=i+1;j<place;j++) //place is the number of elements in the array of strings
             {
                 if(strcmp(global[i], global[j]) > 0) {
                    strcpy(buff1, global[i]);
                    strcpy(global[i], global[j]);
                    strcpy(global[j], buff1);
                 }
             }
    This got the sort working perfectly except the very last item it prints is out of place.
    (BTW, the 12's were a mistake - I meant 15.)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, you've run into my classic "drop the last parenthesis" bug:
    Code:
    len = strlen(Global[i]; //the bug!
    
    len = strlen(Global[i]); //the fix :)
    You can't assign anything to Global[Anyrow][15], because it goes from 0 to 14, only.

    strcpy() All your strings should already have an end of string char. No added char is good, here.

    If you have a problem with char's not having an end of string char, then let's add that, up above the sort.
    Last edited by Adak; 02-22-2011 at 09:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array structures
    By Jaxtomtom89 in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 05:09 AM
  2. Sorting an array of strings
    By porstart in forum C Programming
    Replies: 2
    Last Post: 11-21-2010, 08:46 PM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 11:28 AM
  4. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-21-2003, 11:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21