Skip to content

Incorrect sorting when using a comparator attribute that is undefined on the model #30

@melnikov-s

Description

@melnikov-s
var Model = require('ampersand-model');
var Collection = require('ampersand-collection').extend({model: Model, comparator: 'attribute'});


var models = [new Model, new Model, new Model];
var collection = new Collection();
collection.reset(models);
console.log(models.map(function(m){return m.cid}));
console.log(collection.models.map(function(m){return m.cid}));

results:
['state1','state2','state3']
['state3', 'state2', 'state1']

Models will get sorted in reverse order when the comparator attribute is undefined on a model.

This result can be replicated in chrome and firefox but not phantom js.

This works correctly in Backbone.Collection as it uses the underscore sortBy method and not Array.prototype.sort .

Swapping these two lines https://github.com/AmpersandJS/ampersand-collection/blob/v1.3.17/ampersand-collection.js#L227-228 (and https://github.com/AmpersandJS/ampersand-collection/blob/v1.3.17/ampersand-collection.js#L235-236) seems to do the trick for chrome and firefox but breaks phantom js.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions