How do you make sortable tables with a pager with data from a custom table? For Drupal 6 you could do something like this:

$header = array(
  array('data' =&gt; t('Order id'), 'field' =&gt; 'order_id'),
  ...
  array('data' =&gt; t('Transaction time'), 'field' =&gt; 'payment_time', 'sort' =&gt; 'desc'),
);
$sql = "...";
$sql .= tablesort_sql($header);
$limit = 25;
$result = pager_query($sql, $limit);
...


I took a look and for drupal 7 and both pager_query and tablesort_sql is now gone. It seems that instead the PagerDefault class can be used to create a pager query using DBTNG. I wasn't able to find any clues on a simple API for attaching a sortable table to the query like it is done in Drupal 6.

So how do you create a sortable table with a pager pulling data from a custom table?
You use the so called extenders.  In your case, the code would be similar to the following one.

$header = array(
  array('data' =&gt; t('Order id'), 'field' =&gt; 'order_id'),
  // ...
  array('data' =&gt; t('Transaction time'), 'field' =&gt; 'payment_time', 'sort' =&gt; 'desc'),
);

// Initialize $query with db_select().
$query = db_select('your table name');

// Add the fields you need to query.
// ... 

// Add the table sort extender.
$query = $query-&gt;extend('TableSort')-&gt;orderByHeader($header);

// Add the pager.
$query = $query-&gt;extend('PagerDefault')-&gt;limit(25);
$result = $query-&gt;execute();


See HowTo: Convert a module to DBTNG, Dynamic queries: Table sorting and Extenders.