带有 Lodash 的 Sortby 集合 BackboneJS

Sortby Collection BackboneJS With Lodash

我有这样的合集:

child{length:3, models:array[3], _byId:Object}

我想对模型数组进行排序,我这样使用 lodash:

var array_of_objects = new ListCollection();

var data = _.sortByOrder(array_of_objects.models, ['id'], ['asc']);

我只得到结果:

[child, child, child]

如何使用保持长度和对象对模型数组进行排序。

如果要订购原版collection,请设置collection.comparator to 'id' and then call collection.sort()

要在不影响 collection 的情况下订购模型,请执行以下操作:_.sortBy(collection.models, 'id')

请注意,这些将排序 Models,而不是原生 js 数组。如果您希望对数组的原始 collection 进行操作,请使用 var models = JSON.parse(collection.toJSON()) 获取 collection 的副本,然后按照 _.sortBy.

的说明进行操作