retrieving values from a database in drupal 7
$select = db_select('registered_users', 'c')
-&gt;condition('c.fieldname', $id)  //try the condition if u have anything
-&gt;fields('c');

$result = $select-&gt;execute();
if($result-&gt;rowCount() == 0) return false;

$val = $result-&gt;fetch();


for sorting try asc or desc followed by the condition
you can get the answer in google itself