Categories
JavaScript Answers

How to use Sequelize findAll with sort order in Node.js?

Spread the love

To use Sequelize findAll with sort order in Node.js, we call findAll with an object with the order property.

For instance, we write

const companies = Company.findAll({
  where: {
    id: [46128, 2865, 49569, 1488, 45600, 61991, 1418, 61919, 53326, 61680],
  },
  order: [
    ["id", "DESC"],
    ["name", "ASC"],
  ],
  attributes: ["id", "logo_version", "logo_content_type", "name", "updated_at"],
});

to call findAll with an object with the order property to return the results sorted by id descending and by name ascending.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *