theme_table() isn't respecting sortable fieldname
You're doing too much work.

For sorting:

$query = db_select('node', 'n')
        -&gt;fields('n', array('nid'))
        -&gt;condition('uid', $user-&gt;uid);

$sort_query = $query-&gt;extend("TableSort")-&gt;orderByHeader($headerColumns);
$records = $sort_query-&gt;execute();


Using the TableSort extension will simplify things immensely.