sort array of strings

This is a discussion on sort array of strings within the C Programming forums, part of the General Programming Boards category; Hey guys just wondering if u know how to sort an array of strings without using qsort and all of ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005

    sort array of strings

    Hey guys just wondering if u know how to sort an array of strings without using qsort and all of those. I dont get how all the swapping works and so on?

  2. #2
    Math wizard
    Join Date
    Dec 2006
    All I can think of is to parse the strings until the null character is reached. That is, check to see if the given character has a value of 0. Once reached, use temporary strings (locally-defined would be one method) to store the contents. When done, write the strings in the order as you want. It's one method, but probably a lousy one.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    to compare strings - use strcmp
    to swap strings - strcpy (or if you can mange to build array of pointers to strings - just swap pointers)
    To be or not to be == true

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    This is sample code snap which is used for sorting the array. Well, it just the compare and copy part of the whole sorting code. You should try using this code to build your whole sorting program.

         if( strcmp(str[i], str[j]) > 0)            

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    As vart was saying, if you pointers to strings (for example if the strings were dynamically allocated), you can just swap pointers.
    char *p;
    if(strcmp(str[i], str[j]) > 0) {
        p = str[i];
        str[i] = str[j];
        str[j] = p;
    Also see these pages for actual sorting algorithms.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 11:15 PM
  2. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  3. Sorting
    By vasanth in forum A Brief History of
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-21-2003, 11:15 PM

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