Is it possible to do multiple orderBy() columns?
knex
.select()
.table('products')
.orderBy('id', 'asc')
The orderBy() chainable only takes a single column key and a sort value, but how can I order by multiple columns?
Is it possible to do multiple orderBy() columns?
knex
.select()
.table('products')
.orderBy('id', 'asc')
The orderBy() chainable only takes a single column key and a sort value, but how can I order by multiple columns?
The Knex orderBy function also receives an array:
or
or
You can use the following solution to solve your problem:
You can call
.orderBy
multiple times to order by multiple columns:The original answer is technically correct, and useful, but my intention was to find a way to programatically apply the
orderBy()
function multiple times, here is the actual solution I went with for reference:Knex offers a modify function which allows the queryBuilder to be operated on directly. An array iterator then calls
orderBy()
multiple times.