Well, I've decided to use the OS's function of sorting which is the qsort(). But after implementing it, it doesn't seem to sort at all. Here's a snippet which is shortened alot :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXSIZE 100
typedef struct dataTuple
{
char name[12];
char server[30];
}Tuple;
int compare(const void * p1, const void *p2);
int main(int argc, char *argv[])
{
Tuple *data[6]; /* I've decided to test it with a small value first */
char line[100];
int counter, length, i, j=0,k;
/* does file reading and have an array of pointers pointing to struct*/
/* Note : its rather a big chunk of code which I don't think its the problem
as I've used a tokenizer so I removed it from here */
for(k=0;k<=6;k++)
{
printf("Data : %s\n", data[k]->server);
}
/* Using quicksort method and sort it by server*/
qsort(data,6,sizeof(Tuple),compare);
for(k=0;k<=6;k++)
{
printf("Data : %s\n", data[k]->server);
free(data[k]);
}
return 0;
}
int compare(const void *p1, const void *p2)
{
const char *server1 = (const char *)(((Tuple *)p1)->server);
const char *server2 = (const char *)(((Tuple *)p2)->server);
return (strcmp(server1,server2));
}
I printed before sort and after sort, it doesn't seem to sort at all. I'm not sure what's the problem here.
*EDIT*
Thanks for the various input guys on the previous posts