knockout.viewmodel
knockout.viewmodel copied to clipboard
Sorting On a Field
I have an observable array, domains, which is an array of Objects and I am trying to sort it on a name property in the objects after pushing to the array following an AJAX call:
viewModel.domains.pushFromModel(data); viewModel.domains.sort( function(left,right) { return left.name == right.name ? 0 : (left.name < right.name ? -1 : 1); });
This doesn't seem to work.
I figured out the problem:
In my return statement, I needed to respect that the properties are observables:
return left.name() == right.name() ? 0 : (left.name() < right.name() ? -1 : 1);