Last Updated: February 25, 2016
·
2.827K
· dangaytan

Sort rows in Postgresql by a list of values

Use the POSITION function for strings:

SELECT *
FROM table
WHERE ID IN (1, 2, 3, 4, 5)
ORDER BY POSITION(id::text IN '2, 1, 5, 3, 4')

Cheers.