/*
- * Copyright (C) 2011-2014 jeanfi@gmail.com
+ * Copyright (C) 2011-2015 jeanfi@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
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();
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) {