Smile for the Camera: a new cybercrime short story ebook.

2.4 Sorting Data

Once you've loaded your data you can sort it using:

\dtlsort[replacement list]{criteria}{db-name}{handler}

where ⟨db-name⟩ is the label that identifies the database. The ⟨criteria⟩ argument is a comma-separated list of column labels that indicate the sort order. For example, if you first want to sort on the surname column and then on the forenames column the ⟨criteria⟩ should be surname,forenames (make sure you don't have any unwanted spaces in the list). You can optionally add =order⟩ after the column label where ⟨order⟩ is either ascending or descending. If omitted, ascending is assumed. For example, to sort in descending order, first by surname and then by forenames, the ⟨criteria⟩ should be:

The ⟨handler⟩ argument is a control sequence that's used for the comparisons. The datatool package comes with four handlers:
  1. A case-sensitive comparison:

  2. A case-insensitive comparison:

  3. English word-ordering comparison (as described by the Oxford Style Manual [74]):

  4. English letter-ordering comparison:

The last two are intended for indexes. If you want any further details about those handlers, see the datatool user guide [95]. The first two handlers, \dtlcompare and \dtlicompare, are the ones you're most likely to need for administrative purposes. The datatool package provides convenient shortcuts:

\DTLsort[replacement list]{criteria}{db-name}

which uses \dtlcompare and

\DTLsort*[replacement list]{criteria}{db-name}

which uses \dtlicompare.

The optional argument ⟨replacement list⟩ is provided in case null values are encountered. You're unlikely to have null values if you load your data from a CSV file, but you may have null values if you use datatooltk to fetch data from a SQL database.

For example, suppose you want to sort your data according to the author column, but if there's a null value in that column then sort it by the title column, you would need to do


(where books is the label identifying your data.)

TeX isn't designed for data analysis, so sorting your data within your document in this way isn't very efficient. It's therefore better to sort your data before you load it in your document. This can be done using datatooltk's --sort option (in batch mode) or via the ToolsSort menu item (in GUI mode). Alternatively, if you're fetching data from a SQL database, it's more efficient to append the ORDER BY statement to your SELECT statement.


  1. Fetch and sort data from a CSV file.

  2. Fetch and sort data from a SQL database:

    datatooltk --output books.dbtex --sqluser sampleuser --sqldb samples --sql "SELECT * FROM books ORDER BY author"

    and then in your document:


This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs