How to set the Sequelize findAll sort order in Node.js?

Sometimes, we want to set the Sequelize findAll sort order in Node.js.

In this article, we’ll look at how to set the Sequelize findAll sort order in Node.js.

How to set the Sequelize findAll sort order in Node.js?

To set the Sequelize findAll sort order in Node.js, we can set the order property.

For instance, we write

const getStaticCompanies = () => {
  return Company.findAll({
    where: {
      //...
    },
    order: [
      ['id', 'DESC'],
      ['name', 'ASC'],
    ],
    attributes: ['id', 'logo_version', 'logo_content_type', 'name', 'updated_at']
  });
};

to call Company.findAll with an object that has the order property set an array to the columns and how we want to order them.

Conclusion

To set the Sequelize findAll sort order in Node.js, we can set the order property.