Skip to content

Instantly share code, notes, and snippets.

@ecto
Created January 24, 2011 22:45
Show Gist options
  • Select an option

  • Save ecto/794140 to your computer and use it in GitHub Desktop.

Select an option

Save ecto/794140 to your computer and use it in GitHub Desktop.
function byRating(a, b) {
var x = a[1];
var y = b[1];
return y - x;
}
app.get('/top', function(req, res){
db.keys('image:*:location', function(err,keys){
if (err) { throw err; }
var images = [];
var originalCount = 0;
var returnCount = 0;
keys.forEach(function(key){
var split = String(key).split(':');
originalCount++;
db.mget('image:' + split[1] + ':location',
'image:' + split[1] + ':rating',
function(err, image){
images.push(image);
returnCount++;
if (originalCount == returnCount) {
images.sort(byRating);
res.render('top.ejs', {
locals: {
images: images
}
});
}
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment