Browse Source

r.stats: fix qsort cmp fn (see https://trac.osgeo.org/grass/ticket/3564)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73137 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 6 years ago
parent
commit
3a3413a0fe
1 changed files with 14 additions and 6 deletions
  1. 14 6
      raster/r.stats/stats.c

+ 14 - 6
raster/r.stats/stats.c

@@ -183,14 +183,18 @@ int update_cell_stats(CELL ** cell, int ncols, double area)
 static int node_compare(const void *pp, const void *qq)
 {
     struct Node *const *p = pp, *const *q = qq;
-    register int i, x;
+    register int i;
     register const CELL *a, *b;
 
     a = (*p)->values;
     b = (*q)->values;
-    for (i = nfiles; --i >= 0;)
-	if (x = (*a++ - *b++), x)
-	    return x;
+    for (i = nfiles; --i >= 0;) {
+	if (*a < *b)
+	    return -1;
+	else if (*a > *b)
+	    return 1;
+	a++, b++;
+    }
 
     return 0;
 }
@@ -203,7 +207,9 @@ static int node_compare_count_asc(const void *pp, const void *qq)
     a = (*p)->count;
     b = (*q)->count;
 
-    return (a - b);
+    if (a < b)
+	return -1;
+    return (a > b);
 }
 
 static int node_compare_count_desc(const void *pp, const void *qq)
@@ -214,7 +220,9 @@ static int node_compare_count_desc(const void *pp, const void *qq)
     a = (*p)->count;
     b = (*q)->count;
 
-    return (b - a);
+    if (a > b)
+	return -1;
+    return (a < b);
 }
 
 int sort_cell_stats(int do_sort)