[plug] qsort

Richard wpickett at iprimus.com.au
Sun Jun 9 16:39:08 WST 2002


On Sun, 2002-06-09 at 15:48, Bernard Blackham wrote:
> James,
> 
> Are you talking about using the qsort function in C/C++? Though not
> quite on topic, but here goes :)
> 
> 	int some_array[50] = {...... fill in the array .....};
> 	qsort(some_array, 50, sizeof(int), int_cmp);
> 
> where int_cmp is your comparision function - takes two data elements
> (they dont have to be ints), and returns 0 if they're equal, or a
> negative or positive number depending if one is bigger or smaller
> than the other. if you're not sorting strings you usually have to
> define it yourself, eg:
> 
> 	int int_cmp(const void* a, const void* b) {
> 		if (*(int*)a < *(int*)b) return -1;
> 		if (*(int*)a == *(int*)b) return 0;
> 		return 1; /* otherwise a>b */
> 	}
> 
> Note the awkward typecasting. You can do something similar for any
> data type. strcmp will do strings for you.
> 
> http://www.cplusplus.com/ is a great resource (well it's the one I
> recommend to people and they love it).
> 
> HTH,
> 
> Bernard.
Wouldn't it be easier just to write your own simple sort. This all seems
a bit convoluted to me :)



More information about the plug mailing list