I am fetching data from an SQL database given a list of primary keys; the data returned is not always in the same order and I need to re-order them to preserve the original array index.
For example :
var ids = [2, 12, 5, 3, 10];
var query = 'SELECT * FROM tbl_foo WHERE id IN (' + ids.join(',') + ')';
db.query(query, null, function (result) {
console.log(result.rows);
// [ { id: 2, ... },
// { id: 3, ... },
// { id: 5, ... },
// { id: 10, ... },
// { id: 12, ... } ]
});
Note: nevermind the SQL injection warnings, this is an example; KISS.
How would I preserve that order, or would I efficiently order the resultset in the given original one?
Thanks!