From 5e8a9db72b7db4795cbbc77e562454919c603993 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Wed, 24 Feb 2010 08:10:41 +0100 Subject: [PATCH] Finished the routine to print in size order. --- dus.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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); } -- 2.20.1