title: How to order data in sqlalchemy by list
body: <p>I have list of ID from external postgresql database.</p>
  
  <pre><code>A = [1,2,3,4,5,6,7,98,0]
  </code></pre>
  
  <p>I would to do query to database using SQLAlchemy, but I would to sort data in postgresql by A list.</p>
  
  <p>I have read a lot of documentation but cannot find any suggestions how to do that.</p>
  
  <p>So, in final I would to have:</p>
  
  <pre><code>results = session.query(user).limit(20).offset(10).order_by(A)
  </code></pre>
  
  <p>Cheers</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I found solution, it's not so good as I expected, but works well. Anyway if you know better solution, just let me know!</p>
  
  <pre><code>ids = ','.join([str(i) for i in A])
  results = session.query(user).filter(user.id.in_(A)).\
  limit(20).offset(10).\
  order_by(" position(CONCAT(',',users.id::text,',') in ',{0},'.format(ids)")
  </code></pre>