javascript - Knockout.js: Sort on multiple keys -


in knockout.js, possible sort multiple keys?

for example:

var anotherobservablearray = ko.observablearray([ { name: "bungle", type: "bear" }, { name: "boogle", type: "bear" }, { name: "george", type: "hippo" }, { name: "zippy", type: "unknown" } ]); 

for example, might want sort asc type first, , desc name. possible ko observable arrays?

thanks

i extend observable arrays sortby method lets me define field sort sorting direction:

ko.observablearray.fn.sortby = function(fld, direction) {     var isdesc = direction && direction.tolowercase() == 'desc';      return ko.observablearray(this.sort(function (a, b) {         = ko.unwrap(a[fld]);         b = ko.unwrap(b[fld]);          return (a == b ? 0 : < b ? -1 : 1) * (isdesc ? -1 : 1);         })); }; 

then when binding:

<div data-bind="foreach: yourarray.sortby('type', 'asc')"> 

see fiddle


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -