sort the list of version in decrease order in the package html pages.
authorJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 3 Nov 2014 09:54:20 +0000 (10:54 +0100)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 3 Nov 2014 09:54:20 +0000 (10:54 +0100)
NEWS
src/html.c

diff --git a/NEWS b/NEWS
index 6fecf53..3315032 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,8 @@ v1.3.3
 ------
 
 * added total number of downloads in the package html pages.
+* sort the list of version in decrease order in the package html
+  pages.
 
 v1.3.2
 ------
index 83c6fb6..1ba96ad 100644 (file)
@@ -199,11 +199,42 @@ static json_object *distro_to_json(struct distro_stats *d)
        return json;
 }
 
+static int version_cmp(const void *o1, const void *o2)
+{
+       struct version_stats **v1, **v2;
+
+       v1 = (struct version_stats **)o1;
+       v2 = (struct version_stats **)o2;
+
+       return (*v1)->date_created <= (*v2)->date_created;
+}
+
+static struct version_stats **sort_versions(struct version_stats **vers)
+{
+       size_t n;
+       struct version_stats **tmp, **result;
+
+       tmp = vers;
+       n = 0;
+       while(*tmp) {
+               n++;
+               tmp++;
+       }
+
+       result = malloc((n + 1) * sizeof(struct version_stats *));
+       memcpy(result, vers, n * sizeof(struct version_stats *));
+       result[n] = NULL;
+
+       qsort(result, n, sizeof(struct version_stats *), version_cmp);
+
+       return result;
+}
+
 static json_object *
 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
 {
        json_object *json, *json_versions, *json_distros, *json_distro;
-       struct version_stats **versions;
+       struct version_stats **versions, **tmp;
        struct distro_stats **distros, *d;
 
        json = json_object_new_object();
@@ -219,14 +250,16 @@ pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
 
        json_versions = json_object_new_array();
        json_object_object_add(json, "versions", json_versions);
-       versions = pkg->versions;
-       while (*versions) {
+       versions = sort_versions(pkg->versions);
+       tmp = versions;
+       while (*tmp) {
                json_object_array_add
                        (json_versions,
-                        json_object_new_string((*versions)->version));
+                        json_object_new_string((*tmp)->version));
 
-               versions++;
+               tmp++;
        }
+       free(versions);
 
        distros = pkg->distros;
        if (distros) {