is there a way to automatically sort an observable array when a bound property is changed? I believe in the following example, my view is updated when i add a new person, but can I get the view to refresh and apply my sorting function if one of the person’s age is changed?
person = {
age: ko.observable();
}
viewModel = {
people: ko.observableArray([]),
someSortFunction: function() {
this.people.sort(function(person1, person2) {
return person2.age() - person1.age();
});
}
}
<div data-bind="foreach: people">
<span data-bind="text: age"/>
</div>
You can subscribe an observable and act whenever that observable changes: