Sorting model by property in belongsTo


#1

Hi guys

I have a model with the following list of products…

{
    products: [
        {
            id: 1,
            something: "value",
            properties: {
                name: "Name1"
            }
        },
        {
            id: 2,
            something: "value",
            properties: {
                name: "Name3"
            }
        },
        {
            id: 3,
            something: "value",
            properties: {
                name: "Name2"
            }
        }
    ]
}

If I were to sort on these products by sorting on the “product.properties.name” how would I do that? My first idea was to use the sortableMixin and using the following snippet:

sortProperties: ['properties.name'],
sortAscending: true

But that doesn’t really product the result I intended. How should I go about this?


#2

You could try out the Ember.computed.sort method and do something like:

sortedProducts: Ember.computed.sort("products", (a, b) => {
    if (a.properties.name < b.properties.name) {
        return -1;
    } else if (a.properties.name > b.properties.name) {
        return 1;
    }

    return 0;
})