0

I have an array of two ids:

placeids = [22,14]

Then I have this query:

models.Places.findAll({
            where: {
                id: {in: [ placeids ]}
            }

    }).then(function (places) {
        response(places).code(200);
    }, function (rejectedPromiseError) {
        response(rejectedPromiseError).code(401);
    });       

I want the result to return exact records, the way I have requested them, in my case 22 and then 14.

Sequelize return them, but it orders them in descending. So in my case it returns 14 and 22.

How can I address this?

elreeda
  • 4,525
  • 2
  • 18
  • 45
Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • I doubt Sequelize orders them, it's the SQL database that does so. Being able to influence that order depends on whether or not the database supports such a thing (MySQL does, see [this answer](http://stackoverflow.com/a/396771/893780)). – robertklep Jul 04 '16 at 12:05

1 Answers1

1

You can pass sequelize an order parameter:

models.Places.findAll({
    where: {
        id: {in: [placeids]}
    },
    order: 'id DESC'
});

Or, you can do the ordering manually:

.then(function (places) {
    places.sort(function (x, y) {
        return placeids.indexOf(x.id) > placeids.indexOf(y.id)
    });
    return places;
});
Adam
  • 4,985
  • 2
  • 29
  • 61