LP1821032 - compare_array implmenetation is confusing user/abellenir/lp1821032-compare_array-implmenetation-is-confusing
authora. bellenir <abelleni@grpl.org>
Wed, 20 Mar 2019 15:51:58 +0000 (11:51 -0400)
committera. bellenir <abelleni@grpl.org>
Wed, 20 Mar 2019 15:51:58 +0000 (11:51 -0400)
the existing implementation works but can much more concise.

because it uses localeCompare, this suggested implementation will only work on arrays of strings.
it can be modified to work on arrays of numbers too, if needed.

Signed-off-by: a. bellenir <abelleni@grpl.org>
Open-ILS/web/js/ui/default/staff/cat/services/holdings.js

index d78c049..ab2d681 100644 (file)
@@ -90,22 +90,10 @@ function(egCore , $q) {
                 svc.copies = svc.copies.sort(
                     function (a, b) {
                         function compare_array (x, y, i) {
-                            if (x[i] && y[i]) { // both have values
-                                if (x[i] == y[i]) { // need to look deeper
-                                    return compare_array(x, y, ++i);
-                                }
-
-                                if (x[i] < y[i]) { // x is first
-                                    return -1;
-                                } else if (x[i] > y[i]) { // y is first
-                                    return 1;
-                                }
-
-                            } else { // no orgs to compare ...
-                                if (x[i]) return -1;
-                                if (y[i]) return 1;
-                            }
-                            return 0;
+                            // the longer array "wins" if we run off the end of either:
+                            if(i >= x.length || i >= y.length){ return y.length - x.length; }
+                            // return a value if we find a difference, otherwise recurse:
+                            return x[i].localeCompare(y[i]) || compare_array(x,y,i+1);
                         }
 
                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);