Backbone/Underscore sortBy is not sorting collection

前端 未结 3 1448

I have a list of users (six to be exact) in a collection with \'firstname\', \'lastname\' properties. Doing a fetch, the comparator below sorts them by \'firstname\', and it wor

3条回答
  •  长情又很酷
    2021-02-04 15:58

    Underscore's sortBy which Backbone uses, returns the sorted collection not sort it in place... To illustrate:

    var flinstones = [{first: 'Baby', last: 'Puss'}, {first: 'Fred', last: 'Flinstone'}];
    var sorted = _.sortBy(flinstones, function (character) { return character.last ; });
    console.log(sorted);
    console.log(flinstones);
    

提交回复
热议问题