I want the result set of a database query to have a certain order. The information I want to order by is not contained in the database, but dynamically generated in code (so I cannot use ORDER BY).
Is there a way to sort a dataset after executing the database query? (I don’t need indexed access but only want to iterate over all records.)
There is a possibility that shares similarities with Jens’ answer (+1) but gets to the result in a slightly different fashion.
Given an existing table:
If you know the desired short order (either by processing the table or some query result from it), create a temp table that has some key value to match the desired rows from the original table and then the sort order data:
Set the
sortvaluefield in the temp table to contain the desired order (it could be any sortable data type – doesn’t have to be integer):Then generate the results with a join against the table that provides the sort order: