Thread: Alphabetically sorting argv[]

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

    Alphabetically sorting argv[]

    I'm trying to alphabetically sort my argv[] array, which will hold char pointers(strings) from the command line. This is my code, but it doesn't seem to be doing anything. Anyone see whats wrong?

    Code:
    int sort_function( const char *a, const char *b) //this is the actual compare function!
     {
        return(strcmp(a,b));
     }
    
    int main (int argc, char *argv[]) {
          qsort(argv, sizeof(argv), sizeof(argv[0]), sort_function);
          for (x = 1; x < 4; x++)
              printf("%s\n", argv[x]);
    }
    Last edited by serg_yegi; 03-27-2010 at 05:32 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    sizeof(argv) is almost certainly 4 irrespective of how many things you actually have. argc will tell you how many things you need to sort.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Do I need to assign qsort to something, because even with the argc replacement, I get the same incorrect results.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    qsort returns void, so there's nothing there. I am (suprisingly to me at least) getting utter meaninglessness when I print the strings inside the comparator:
    Code:
    >temp this that a and b
    Called with string 39♥ and -9♥, returning 1
    Called with string 99♥ and 39♥, returning 1
    Called with string ?9♥ and 99♥, returning 1
    Called with string B9♥ and ?9♥, returning 1
    Called with string G9♥ and B9♥, returning 1
    Called with string 39♥ and -9♥, returning 1
    Called with string 99♥ and 39♥, returning 1
    Called with string ?9♥ and 99♥, returning 1
    Called with string B9♥ and ?9♥, returning 1
    Called with string 39♥ and -9♥, returning 1
    Called with string 99♥ and 39♥, returning 1
    Called with string ?9♥ and 99♥, returning 1
    Called with string 39♥ and -9♥, returning 1
    Called with string 99♥ and 39♥, returning 1
    Called with string 39♥ and -9♥, returning 1
    this
    that
    a
    and
    b
    More details if and when I figure something out.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay after printing absolutely everything out, I was reminded of what I failed to parse from the description of qsort:
    Quote Originally Posted by ISO C standard
    The contents of the array are sorted into ascending order according to a comparison
    function pointed to by compar, which is called with two arguments that point to the
    objects being compared.
    (emphasis mine)
    so what your comparator receives are two void *, which point to the data and are therefore functionally char **. So your comparator should look like
    Code:
    int sort_function( const void *a, const void *b) //this is the actual compare function!
     {
        char **first = a;
        char **second = b;
        return(strcmp(*first,*second));
     }
    You're going to get warnings about dumping the const, which you can fix or ignore at your discretion.

    (You'll also want to be careful, when sorting, not to mix the name of the program at argv[0] into the mix as well.)

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Thanks a lot. Is there anyway you can top me on how to avoid argv[0]? Everything I've tried so far has produced errors.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Start sorting with argv[1].

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Nevermind, got it. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an array alphabetically?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 03-09-2011, 04:53 AM
  2. sorting alphabetically
    By ahming in forum C Programming
    Replies: 8
    Last Post: 05-26-2005, 10:34 AM
  3. Sorting Alphabetically
    By orgy230 in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 02:28 PM
  4. Sorting inputed letters alphabetically...
    By IanelarAzure in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2002, 08:56 PM