db3-order-by
v1.0.2
Published
Sorting rules in JSON format
Downloads
3
Readme
Usage
var orderBy = require('db3-order-by')
orderBy.query(sortingRule)
//returns corresponding sql `order by` clause
orderBy.sort(sortingRule)
//returns compare function, usable for Array.sort
SQL order by
orderBy.query('id')
// returns `id`
orderBy.query({id: 'desc'})
// returns `id` desc
orderBy.query({id: 'desc', name: 'asc'})
// returns `id` desc, `name` asc
orderBy.query(['id', {name: 'desc'}])
// returns `id`, `name` desc
Array.sort
var fruit = [
{id: 1, name: 'Banana'},
{id: 2, name: 'Apple'},
{id: 3, name: 'Apple'}
]
fruit.sort(orderBy.sort('name'))
// fruit will be
// [{id: 2, name: 'Apple'}, {id: 3, name: 'Apple'}, {id: 1, name: 'Banana'}]
fruit.sort(orderBy.sort({id: 'desc'}))
// fruit will be
// [{id: 3, name: 'Apple'}, {id: 2, name: 'Apple'}, {id: 1, name: 'Banana'}]
fruit.sort(orderBy.sort(['name', {id: 'asc'}]))
// fruit will be
// [{id: 2, name: 'Apple'}, {id: 3, name: 'Apple'}, {id: 1, name: 'Banana'}]
When is this useful?
If you want to use the same sorting rules for js Array.sort
and SQL order by
.