Sorting a 2-dimensional char Array?

I need some sample code that will show me how to sort a two-dimensional character array. Nothing fancy. I just have an array of names I want in  alphabetical order. char Names[9][33];
scully12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
Just use the qsort() function.

example follows.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chensuCommented:
int compare(const void *elem1, const void *elem2)
{
    return strcmp((const char *)elem1, (const char *)elem2);
}

const int x = 9, y = 33;

char Names[x][y];

//...

qsort(Names, x, y, compare);
0
nietodCommented:
int CmpFun(const void *P1,const void *P2)
{
   return strcmp((char *)P1,(char *)P2);
};



char Names[9][33];

qsort(Names,33,9,CmpFun);
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

jasonclarkeCommented:
one other thing you might consider, especially if you want to do anything else with the data is to read it into some more capable data structures, e.g. using the STL:

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

char Names[9][33];

vector<string>  svec;
for (int i=0; i<9; i++)
{
    svec.push_back(Names[i]);
}

std::sort(svec.begin(), svec.end());

....
0
nietodCommented:
Probably not in keeping with the "nothing fancy" part of the question, but fancy is worth considering...
0
jasonclarkeCommented:
maybe you're right, it just depends on what is wanted next.  I would rather have a vector of strings than a C array of char*s for most purposes.

Also note that in terms of lines of code written (if you exclude the #includes) its about the same.
0
nietodCommented:
I agree totally.  There is no question that I would do it that way (Unless I needed non-C++ compatibility.)
0
chensuCommented:
nietod,

Just noticed that your code

qsort(Names,33,9,CmpFun);

should read

qsort(Names,9,33,CmpFun);
0
nietodCommented:
Opps.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.