The text of this article is not in this database — only its details are. Read it on the old site: Apply Sort Function on Observable Array Using KnockoutJS
1 Comment
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.

Craig GersPosted Jun 10, 2022, 3:17 PM
Thanks for your great article. An alternative approach which I think may be better is to introduce a sort function on the data-bind, that way no matter how the array object changes or updates, the UI will always sort it. An example of this can be found at https://jsfiddle.net/Craig_Gers/3q26vo4y/ The magic happens on the foreach data bind <ul data-bind="foreach: trainee.sort(function(l, r) { let x = l.name.toLowerCase(); let y = r.name.toLowerCase(); if(x < y) {return -1;} if(x > y) {return 1;} return 0; })"> . No button required to sort. You can add any sort function that you require.