Thread: How do you sort a 2 Dimensional Array?

  1. #1
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17

    How do you sort a 2 Dimensional Array?

    I was just wondering how do I sort a 2 dimensional array?

    char testArray[5][11];
    strcpy( testArray[0], "hello");
    strcpy( testArray[1], "Hello");
    strcpy( testArray[2], "1hello");
    strcpy( testArray[3], "2hello");
    strcpy( testArray[4], "Ello");

    Thus the sorted will be...
    1hello
    2hello
    Ello
    Hello
    hello

    Anyone know how to do this?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Figure out how to compare two strings with strcmp(), and then sort your array with some algorithm, like insert sort.

  3. #3
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    I got it to sort by ASCII order... But How do I sort by Alphabetical Ascii?

    Such as...
    instead of: A, B, C, D, a, b, c, d
    it will be: A, a, B, b, C, c, D, d

  4. #4
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    Oh, nevermind, its sorted by Ascii.

    Thanks for the suggestion of strcmp

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In case you wanted to have in-case-sensitive string comparison, there are a few things you could do:
    • Use the non-standard function stricmp(). Quite a few people recommend this, but I don't -- namely because it is not standard. Borland might have it, but that doesn't mean my compiler does.
    • Convert all the strings to lower- or upper-case. Not good if you want to get the strings again with mixed case. (See what happened to DOS when they tried this! . . . .)
    • Write your own string comparison function. This is the best idea. And really, it's not that hard.
      Code:
      int case_insensitive_strcmp(const char *one, const char *two) {
          char a, b;
      
          do {
              a = tolower(*one);
              b = tolower(*two);
      
              if(a < b) return -1;
              else if(a > b) return 1;
          } while(*one++ && *two++);
      
          return 0;
      }
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf confusion, 2 dimensional array modification
    By Leojeen in forum C Programming
    Replies: 23
    Last Post: 10-19-2008, 10:58 PM
  2. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 05:27 AM
  3. Two Dimensional Array Input from Formatted Data
    By teedoff087 in forum C Programming
    Replies: 14
    Last Post: 04-29-2007, 01:46 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM