Dynamic Sortable Table with custom query

If sorting is why you’re using a custom query, the end of this thread might be useful. There’s an example of using query builder with dynamic sorting.