tsort in action

Tsort is a table column sort for non-sql tables. It allows you to sort a table by any column heading, where the table data is not directly pulled from a database, but is simply in an array of rows. It does this by providing a new function, tsort_nonsql_sort(), which will sort your rows for you according to the column which has been clicked.

Example Usage

The following example shows a full example of usage. The function which is provided by this module is the following one:

  tsort_nonsql_sort($rows, $sort, $column);

All the rest of the sample code below is standard Drupal coding for a table which
has rows and can be themed.

/**
 * Create a sorted version of the players table
 * The players table has the following columns:
 * - uid, name, rating, played, won, lost, drawn, rating, rating change, current
 */
function gamer_players() {
  $sql = "SELECT uid FROM {users} ORDER BY name";
  // Converted to the D7 database API syntax.
  $result = db_query($sql);
  foreach ($result as $record) {
    if ($record->uid <> 0) {
      $player_stats = gamer_load_user_stats($record->uid);
      $rows[] = array(
          'uid' => $record->uid,
          'name' => "<a href='" . url("vchess/player/" . $player_stats['name']) . "'>" . $player_stats['name'] . "</a>",
          'rating' => $player_stats['rating'],
          'played' => $player_stats['played'],
          'won' => $player_stats['won'],
          'lost' => $player_stats['lost'],
          'drawn' => $player_stats['drawn'],
          'rating_change' => $player_stats['rchange'],
          'current' => $player_stats['current']
      );
    }
  }

  $header = array(
      array('data' => t('uid'), 'field' => 'uid'),
      array('data' => t('name'), 'field' => 'name'),
      array('data' => t('rating'), 'field' => 'rating'),
      array('data' => t('played'), 'field' => 'played'),
      array('data' => t('won'), 'field' => 'won'),
      array('data' => t('lost'), 'field' => 'lost'),
      array('data' => t('drawn'), 'field' => 'drawn'),
      array('data' => t('rating change'), 'field' =>'rating_change'),
      array('data' => t('current'), 'field' => 'current')
  );
  
  // getting the current sort and order parameters from the url
  // e.g. q=vchess/my_current_games&sort=asc&order=White
  $order = tablesort_get_order($header);
  $sort = tablesort_get_sort($header);
  
  // sort the table data accordingly
  $rows = tsort_nonsql_sort($rows, $sort, $order['sql']);  // !!!! Tsort !!!!
  
  $table['header'] = $header;
  $table['attributes'] = array();
  $table['caption'] = array();
  $table['colgroups'] = array();
  $table['sticky'] = "";
  $table['empty'] = "The message to display in an extra row if table does not have any rows.";
  $table['rows'] = $rows;

  return theme_table($table);
}

Project information

Releases