Skip to content
Snippets Groups Projects
Commit 9f47569a authored by rswindell's avatar rswindell
Browse files

Added str_list sorting functions.

parent d8b66f2d
No related branches found
No related tags found
No related merge requests found
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
* Note: If this box doesn't appear square, then you need to fix your tabs. * * Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/ ****************************************************************************/
#include <stdlib.h> /* malloc */ #include <stdlib.h> /* malloc and qsort */
#include "str_list.h" #include "str_list.h"
str_list_t strListAlloc() str_list_t strListAlloc()
...@@ -85,6 +85,46 @@ str_list_t strListAdd(str_list_t* list, char* str) ...@@ -85,6 +85,46 @@ str_list_t strListAdd(str_list_t* list, char* str)
return strListAddAt(list,str,strListCount(*list)); return strListAddAt(list,str,strListCount(*list));
} }
static int strListCompareAlpha(const void *arg1, const void *arg2)
{
return stricmp(*(char**)arg1, *(char**)arg2);
}
static int strListCompareAlphaReverse(const void *arg1, const void *arg2)
{
return stricmp(*(char**)arg2, *(char**)arg1);
}
static int strListCompareAlphaCase(const void *arg1, const void *arg2)
{
return strcmp(*(char**)arg1, *(char**)arg2);
}
static int strListCompareAlphaCaseReverse(const void *arg1, const void *arg2)
{
return strcmp(*(char**)arg2, *(char**)arg1);
}
void strListSortAlpha(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlpha);
}
void strListSortAlphaReverse(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaReverse);
}
void strListSortAlphaCase(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCase);
}
void strListSortAlphaCaseReverse(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCaseReverse);
}
void strListFree(str_list_t* list) void strListFree(str_list_t* list)
{ {
size_t i; size_t i;
......
...@@ -62,6 +62,11 @@ str_list_t strListAddAt(str_list_t* list, char* str, size_t index); ...@@ -62,6 +62,11 @@ str_list_t strListAddAt(str_list_t* list, char* str, size_t index);
/* Count the number of strings in the list and returns the count */ /* Count the number of strings in the list and returns the count */
size_t strListCount(str_list_t list); size_t strListCount(str_list_t list);
void strListSortAlpha(str_list_t list);
void strListSortAlphaReverse(str_list_t list);
void strListSortAlphaCase(str_list_t list);
void strListSortAlphaCaseReverse(str_list_t list);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment