From 8241655178126d9b0a0cf8deba11ee2a060d9adb Mon Sep 17 00:00:00 2001 From: Emmanuel Quentin Date: Thu, 18 Sep 2014 09:58:03 +0200 Subject: [PATCH] Allows to sort on a field --- src/routes.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/routes.js b/src/routes.js index 87f323cff..7c3941b54 100644 --- a/src/routes.js +++ b/src/routes.js @@ -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) { @@ -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)