Knex.js multiple orderBy() columns

You can call .orderBy multiple times to order by multiple columns:

  .orderBy('name', 'desc')
  .orderBy('id', 'asc')

The Knex orderBy function also receives an array:

knex('users').orderBy(['email', 'age', 'name'])


knex('users').orderBy(['email', { column: 'age', order: 'desc' }])


knex('users').orderBy([{ column: 'email' }, { column: 'age', order: 'desc' }])

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:

var sortArray = [
  {'field': 'title', 'direction': 'asc'}, 
  {'field': 'id', 'direction': 'desc'}

  .modify(function(queryBuilder) {
    _.each(sortArray, function(sort) {
      queryBuilder.orderBy(sort.field, sort.direction);

Knex offers a modify function which allows the queryBuilder to be operated on directly. An array iterator then calls orderBy() multiple times.