Sorting an Observable Array in Knockout

后端 未结 4 860
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 12:13

I have an observable array in Knockout of person objects. I wanted to be able to sort the list of persons based on the last name. The problem is the list has a number of duplica

4条回答
  •  面向向阳花
    2021-01-30 13:16

    If you make sure your players.json returns the names sorted, you'll be fine. If it's loading them from a database, you need to add the first name field to your ORDER BY clause.

    If you want to do the sorting in Javascript, you could do it right after you load it from the service:

    players.sort(function(player1, player2) {
        return player1.lastname.localeCompare(player2.lastname) ||
            player1.firstname.localeCompare(player2.firstname);
    });
    

提交回复
热议问题