From: Florian Forster Date: Mon, 21 Jun 2010 08:39:32 +0000 (+0200) Subject: src/utils_array.[ch]: Implement "array_sort". X-Git-Tag: v4.0.0~219 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=18acc4106233bdc59011a0a87d2f4a3373d65c9b;p=collection4.git src/utils_array.[ch]: Implement "array_sort". --- diff --git a/src/utils_array.c b/src/utils_array.c index 5aaabbc..e7ac51e 100644 --- a/src/utils_array.c +++ b/src/utils_array.c @@ -12,6 +12,14 @@ struct str_array_s size_t size; }; +static int sort_callback (const void *v0, const void *v1) /* {{{ */ +{ + const char *c0 = v0; + const char *c1 = v1; + + return (strcmp (c0, c1)); +} /* }}} int sort_callback */ + str_array_t *array_create (void) /* {{{ */ { str_array_t *a; @@ -76,6 +84,16 @@ int array_append_format (str_array_t *a, const char *format, ...) /* {{{ */ return (array_append (a, buffer)); } /* }}} int array_append_format */ +int array_sort (str_array_t *a) /* {{{ */ +{ + if (a == NULL) + return (EINVAL); + + qsort (a->ptr, a->size, sizeof (*a->ptr), sort_callback); + + return (0); +} /* }}} int array_sort */ + int array_argc (str_array_t *a) /* {{{ */ { if (a == NULL) diff --git a/src/utils_array.h b/src/utils_array.h index 917bea9..141dbaa 100644 --- a/src/utils_array.h +++ b/src/utils_array.h @@ -10,6 +10,8 @@ int array_append (str_array_t *a, const char *entry); int array_append_format (str_array_t *a, const char *format, ...) __attribute__((format(printf,2,3))); +int array_sort (str_array_t *a); + int array_argc (str_array_t *); char **array_argv (str_array_t *);