From: a. bellenir Date: Wed, 20 Mar 2019 15:51:58 +0000 (-0400) Subject: LP1821032 - compare_array implmenetation is confusing X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=refs%2Fheads%2Fuser%2Fabellenir%2Flp1821032-compare_array-implmenetation-is-confusing;p=working%2FEvergreen.git LP1821032 - compare_array implmenetation is confusing 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 --- diff --git a/Open-ILS/web/js/ui/default/staff/cat/services/holdings.js b/Open-ILS/web/js/ui/default/staff/cat/services/holdings.js index d78c049bc3..ab2d681681 100644 --- a/Open-ILS/web/js/ui/default/staff/cat/services/holdings.js +++ b/Open-ILS/web/js/ui/default/staff/cat/services/holdings.js @@ -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);