Skip to content

Commit fc90f4a

Browse files
rhaseven7htypicode
authored andcommitted
Added support for multiple fields sorting (typicode#512)
1 parent 5ee5640 commit fc90f4a

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ GET /posts?_sort=views&_order=DESC
158158
GET /posts/1/comments?_sort=votes&_order=ASC
159159
```
160160

161+
For multiple fields, use the following format:
162+
163+
```
164+
GET /posts?_sort=user,views&_order=desc,asc
165+
```
166+
167+
161168
### Slice
162169

163170
Add `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response)

src/server/router/plural.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,18 @@ module.exports = (db, name) => {
144144

145145
// Sort
146146
if (_sort) {
147-
_order = _order || 'ASC'
148-
149-
chain = chain.sortBy(function (element) {
150-
return _.get(element, _sort)
151-
})
152-
153-
if (_order === 'DESC') {
154-
chain = chain.reverse()
147+
if (_sort.match(/,/)) {
148+
const _sortSet = _sort.split(/,/)
149+
const _orderSet = _order.split(/,/)
150+
chain = chain.orderBy(_sortSet, _orderSet)
151+
} else {
152+
_order = _order || 'ASC'
153+
chain = chain.sortBy(function (element) {
154+
return _.get(element, _sort)
155+
})
156+
if (_order === 'DESC') {
157+
chain = chain.reverse()
158+
}
155159
}
156160
}
157161

test/server/plural.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ describe('Server', () => {
4242
{ id: 5, body: 'quux', published: false, postId: 2, userId: 1 }
4343
]
4444

45+
db.buyers = [
46+
{ id: 1, name: 'Aileen', country: 'Colombia', total: 100 },
47+
{ id: 2, name: 'Barney', country: 'Colombia', total: 200 },
48+
{ id: 3, name: 'Carley', country: 'Colombia', total: 300 },
49+
{ id: 4, name: 'Daniel', country: 'Belize', total: 30 },
50+
{ id: 5, name: 'Ellen', country: 'Belize', total: 20 },
51+
{ id: 6, name: 'Frank', country: 'Belize', total: 10 },
52+
{ id: 7, name: 'Grace', country: 'Argentina', total: 1 },
53+
{ id: 8, name: 'Henry', country: 'Argentina', total: 2 },
54+
{ id: 9, name: 'Isabelle', country: 'Argentina', total: 3 }
55+
]
56+
4557
db.refs = [
4658
{ id: 'abcd-1234', url: 'http://example.com', postId: 1, userId: 1 }
4759
]
@@ -269,6 +281,18 @@ describe('Server', () => {
269281
.expect([ db.nested[1], db.nested[0], db.nested[2] ])
270282
.expect(200)
271283
))
284+
285+
it('should sort on multiple fields', () => (
286+
request(server)
287+
.get('/buyers?_sort=country,total&_order=asc,desc')
288+
.expect('Content-Type', /json/)
289+
.expect([
290+
db.buyers[8], db.buyers[7], db.buyers[6],
291+
db.buyers[3], db.buyers[4], db.buyers[5],
292+
db.buyers[2], db.buyers[1], db.buyers[0]
293+
])
294+
.expect(200)
295+
))
272296
})
273297

274298
describe('GET /:resource?_start=&_end=', () => {

0 commit comments

Comments
 (0)