dimanche 19 juin 2016

Ember ranking algorithm

I need to create a simple ranking algorithm in Ember. It needs to take an array of Ember models and produce an ordinal that in case of ties assigns the same rank and skips a position accordingly. The number of items in each array is small, so performance is not a critical consideration. I'd like to maintain Ember idioms where possible.

Example:

// models/competition.js
export default Model.extend({
    competitors: hasMany('competitor')
    ranks: computed.[ranking function here]
});

// models/competitor.js
export default Model.extend({
    name: attr('string'),
    score: attr('number'),
    competition: belongsTo('competition')
    rank: computed.[return rank here]
});

Then, given the following competitors, scores:

Alpha, 100
Beta, 90
BetaPrime, 90
Gamma, 80

I'd like the rank computed property to return as follows:

Alpha, 1
Beta, 2
BetaPrime, 2
Gamma, 4

I can do this in python but I'm new to Ember/Javascript and can't quite figure this out. I'm sure some combination of map/reduce is the key, but am otherwise clueless... Thanks.




Aucun commentaire:

Enregistrer un commentaire