From: Francois Fleuret Date: Wed, 24 Feb 2010 07:10:41 +0000 (+0100) Subject: Finished the routine to print in size order. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=5e8a9db72b7db4795cbbc77e562454919c603993;p=dus.git Finished the routine to print in size order. --- diff --git a/dus.c b/dus.c index 9f05071..536d607 100644 --- a/dus.c +++ b/dus.c @@ -100,6 +100,22 @@ void destroy(struct file_with_size *node) { /**********************************************************************/ +int compare_files(const void *x1, const void *x2) { + const struct file_with_size **f1, **f2; + + f1 = (const struct file_with_size **) x1; + f2 = (const struct file_with_size **) x2; + + if((*f1)->size < (*f2)->size) { + return -1; + } else if((*f1)->size > (*f2)->size) { + return 1; + } else { + return 0; + } +} + + void print_sorted(struct file_with_size *root) { struct file_with_size *node; struct file_with_size **nodes; @@ -117,6 +133,8 @@ void print_sorted(struct file_with_size *root) { nodes[n++] = node; } + qsort(nodes, nb, sizeof(struct file_with_size *), compare_files); + for(n = 0; n < nb; n++) { printf("%u %s\n", nodes[n]->size, nodes[n]->filename); }