sequelize order multiple

Sequelize Order Multiple

If you want to order data in a specific way using Sequelize, you can use the order option. The order option takes an array of arrays or objects where the first element is the column name and the second element is the sorting order.

Example:


    const users = await User.findAll({
      order: [['createdAt', 'DESC'], ['name', 'ASC']]
    });
  

In the example above, we are ordering the data by the createdAt column in descending order and then by the name column in ascending order.

Another way:

You can also use an object instead of an array to specify the column name and sorting order.

Example:


    const users = await User.findAll({
      order: { createdAt: 'DESC', name: 'ASC' }
    });
  

The result of both examples will be the same. It's up to you which syntax you prefer to use.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe