How to order a database query
Something like:

$query = db_select('tablename');
$query-&gt;fields('tablename', array('field1', 'field2'));
$query-&gt;condition('field3', $var1);
$query-&gt;condition('field4', $var2);
$query-&gt;orderBy('field1', 'DESC');

$result = $query-&gt;execute();

foreach($result as $row){
  // Debug
  drupal_set_message('&lt;pre&gt;'.print_r($row,1).'&lt;/pre&gt;');
}


More info: 


https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7
https://api.drupal.org/api/drupal/includes%21database%21select.inc/function/SelectQuery%3A%3AorderBy/7