Page 1 of 1

How to sort a 2D char array using c++ sort() or qsort() Fn?

Posted: Wed Dec 03, 2014 5:02 pm
by Shahidul.CSE
Suppose, I have a 2D array of chars:

Code: Select all

char names[10000][15];
How can I sort it lexicographically using c++ sort() or qsort() function?

I tried in this way:

Code: Select all

#define M 10000;
qsort(names, sizeof(names) / M, M, (int (*)(const void *, const void *)) strcmp);
But it doesn't work. It just gives blanks string, when I check output. What's wrong with this code?

Re: How to sort a 2D char array using c++ sort() or qsort()

Posted: Wed Dec 03, 2014 7:08 pm
by lighted
I sorted words in problem 10815 this way

Code: Select all

char word[50000][210];

int cmp(const void *a, const void *b) {
  
  return strcmp((char *)a, (char *)b);
}

int main() {
  ..
  qsort(word, N, sizeof(word[0]), cmp);
  ..
  return 0;
}
Change to

Code: Select all

sizeof(names[0])