javascript - Call custom array.sort function within another sort function -
this question has answer here:
is there way can this? in code snippet below, this
doesn't have access sortbyname
fn when called within sortbylyncstatus
prototype defn:
sortbyname: function (bud, light) { if (bud.fullname < light.fullname) return -1; if (bud.fullname > light.fullname) return 1; return 0; }, sortbylyncstatus: function (chuck, norris) { if (chuck.lyncavailability == "available" && norris.lyncavailability == "available") { return this.sortbyname(chuck, norris); } else { if (chuck.lyncavailability == "available") return -1; if (norris.lyncavailability == "available") return 1; else { // both away, inactive, offline, or out of office var chucklastactive = $.todatefromjson(chuck.lynclastactive); var norrislastactive = $.todatefromjson(norris.lynclastactive); if (chucklastactive < norrislastactive) return 1; if (chucklastactive > norrislastactive) return -1; return this.sortbyname(chuck, norris); } } }
solution
some.prototype.obj = { sortbyname: function (a, b) { // omitted solution }, sortbylyncstatus: function(a, b) { // compare cases // default name comparison return some.prototype.obj.sortbyname(a, b); } }
this
refer window sorts. unless define sortbyname global function, won't exist method on this
.
you can define function rather method , call directly, so:
var sortbyname = function(a,b){}; var sortbylyncstatus = function(a,b){ return sortbyname(a,b); }
Comments
Post a Comment