Skip to content

Instantly share code, notes, and snippets.

@jcbozonier
Created October 2, 2011 17:13
Show Gist options
  • Save jcbozonier/1257652 to your computer and use it in GitHub Desktop.
Save jcbozonier/1257652 to your computer and use it in GitHub Desktop.
Aggregating the scores by lol and associating with the name of the user who created it.
var aggregate_scores_by_lol = function(votes, lols, users){
var vote_count = {};
var lol_dictionary = {};
for(var index in lols){
var lol = lols[index];
vote_count[lol.id] = 0;
lol_dictionary[lol.id] = lol;
}
for(var vote_index in votes){
var vote = votes[vote_index];
vote_count[vote.lol_id] += 1;
}
var vote_count_keys = [];
for(var vote_count_key in vote_count){
vote_count_keys.push(vote_count_key);
}
var lol_ids = vote_count_keys;
var sorted_lol_ids = lol_ids.sort(function(a, b){
return vote_count[b] - vote_count[a];
});
var lol_scores = [];
for(var lol_id_index in sorted_lol_ids){
var lol_id = sorted_lol_ids[lol_id_index];
var lol = lol_dictionary[lol_id];
lol_scores.push({
id: lol_id,
score: vote_count[lol_id],
url: lol.lol_url,
user_id: lol.user_id,
user_name: users[lol.user_id].name
});
}
return lol_scores;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment