Skip to content

Commit

Permalink
Allows to sort on a field
Browse files Browse the repository at this point in the history
  • Loading branch information
manuquentin committed Sep 23, 2014
1 parent 41af51c commit 8241655
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ routes.list = function(req, res, next) {
// parameters
var _start = req.query._start
var _end = req.query._end
var _sort = req.query._sort
var _sortDir = req.query._sortDir

delete req.query._start
delete req.query._end
delete req.query._sort
delete req.query._sortDir

if (req.query.q) {

Expand Down Expand Up @@ -68,6 +72,18 @@ routes.list = function(req, res, next) {
}
}

if(_sort) {
_sortDir = _sortDir || 'ASC'

array = _.sortBy(array, function(element) {
return element[_sort];
})

if (_sortDir === 'DESC') {
array.reverse();
}
}

// Slice result
if (_end) {
res.setHeader('X-Total-Count', array.length)
Expand Down

0 comments on commit 8241655

Please sign in to comment.