-
sorting...
hi,
I have an array of strings and want to be able to sort them alphabetically. I was wondering if someone could point me in the right direction. Are there any functions to assist me with this or do i have to write my own function from scratch?
thanks, Robert.
-
Hi,
I think that you would find it easier to make your own function which sorts the array alphabetically.
My suggested sorting method is Bubble sort, since its short and easy.
I will experiment with it for a bit and let you know on whether it is successful or unsuccessful.
I hope you get it working
-
Well for simple 'C' style arrays, there's a function called qsort() in stdlib.h
If you're using fancy C++ STL containers, then there is a sort function in <algorithm>
-
thanks... i'm experimenting with qsort now.
-
go to AI Horizon for all the different methods of sorting.
You never know when you might need it.
-
i'm having trouble getting qsort() working...
Code:
char* allsongs[MAXSONGS];
int compare( const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}
qsort((void*)allsongs, 10, sizeof(char*), compare);
this is my modification of the example given in the MSDN. but it gets an access voilation. (btw allsongs is sucessfully filled with about 300 strings)
if i do the following:
Code:
sprintf("%s\n", allsongs[10]);
it outputs a song fine. have i overlooked somthing?
-
Do yourself a favor and have a look at the bubble sorting method, or another one.
Just goto aihorizon.com and then click on Basic CS, there is a whole lot of algorithms for sorting.
-
thanks for all the help.. but i got qsort working. its seems fast enough for what i'm doing.
-
Its always a pleasure sharing some of my knowledge.