brikteknologier / seraph-model

thin model layer for seraph/neo4j (node.js)
MIT License
111 stars 28 forks source link

Custom sort function for composition orderBy #88

Open kevincampanella opened 9 years ago

kevincampanella commented 9 years ago

It would be great to be able to specify a custom sort function for compositions. This could be used for things like sorting on relationship properties. Currently using the "orderBy" composition option, you can sort on a property name only. Please consider allowing the "orderBy" option to take a function to be used as the sort function. Suggest modifying sort-compositions.js, as follows:

    if (typeof comp.orderBy === 'function') {
      model[comp.name].sort(comp.orderBy);
    }
    else {
      var prop = comp.orderBy.property;
      model[comp.name].sort(function(l, r) {
        if (l[prop] < r[prop]) return comp.orderBy.desc ? 1 : -1;
        else if (l[prop] > r[prop]) return comp.orderBy.desc ? -1 : 1;
        else return 0;
      });
    }
andyrichardson commented 7 years ago

Currently using the "orderBy" composition option, you can sort on a property name only.

You can sort by property value - typo? For more complex ordering, you'll need to assign a precedence variable to each node on a case-by-case basis and, following this, order by said precedence variable.

I'm not sure how this could be effectively implemented in future releases of seraph-model. For now, I think your only option is to use a cypher query. There's an example here for custom ordering in neo4j.