knockout.viewmodel icon indicating copy to clipboard operation
knockout.viewmodel copied to clipboard

Sorting On a Field

Open emansouri opened this issue 12 years ago • 1 comments

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.

emansouri avatar Jan 04 '14 15:01 emansouri

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);

emansouri avatar Jan 05 '14 14:01 emansouri