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

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Shahidul.CSE
Experienced poster
Posts: 148
Joined: Sun Jul 13, 2014 4:32 am
Location: Rangpur, Bangladesh

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

Post by Shahidul.CSE » Wed Dec 03, 2014 5:02 pm

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?
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

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

Post by lighted » Wed Dec 03, 2014 7:08 pm

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])
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

Post Reply

Return to “C++”