Help:Sorting/SMW 0.1-0.7

General
Sorting is alphabetic for pagenames, and for attributes of type string. It is numeric for attributes of type integer and float, taking into account units. It is chronological for attributes of type date.

Sort parameter
The sort parameter specifies a relation or attribute that determines the order of the selected pages. Sorting based on a relation or attribute requires that the selection is restricted to pages for which that relation or attribute is defined.

The sortkey is one of the instances of the object or value (there is at least one because of the just mentioned condition; if there are more it seems arbitrary which one is used as sortkey, perhaps just the first in the database). The default sortkey is the name, without namespace prefix, of the selected page. This applies even in the case of enumeration of pages in the query.

The object or value used as sortkey needs not be among the displayed items.

The order parameter can be "ascending" (equal to "asc", default) or "descending" (equal to "desc" or "reverse"). It applies both to the order of the selected pages (in a table: rows) and to the instances of an item to be displayed for a page (in a table: multiple elements in a cell).

The order of the items to be displayed (in a table: columns) is as specified.

Examples (the last one shows that the sort parameter is ignored if the attribute concerned is not used for selection):

less than::+less than::* order=desc: less than::+less than::* sort="less than": less than::+less than::* sort="less than" order=desc: less than::+less than::*

Population:=+Population:=* Population:=+  Population:=+ Population:=*  Population:=*

give:

Population:=+Population:=* Population:=+  Population:=+ Population:=*  Population:=*

Data not available for all pages
Sorting based on a relation or attribute requires that the selection is restricted to pages for which that relation or attribute is defined: if some pages of the desired selection do not have the required annotation, the others cannot be sorted. To have all pages, with those sorted which can be sorted, requires a query giving those sorted that can be sorted, and a second query for the rest; a difficulty in avoiding duplication is that there is no selection criterion "relation/attribute A is undefined". Two possible workarounds are:
 * For the selection of pages for which relation/attribute P is undefined we can set up a special category, or apply the annotation of some relation or attribute for these pages only (see e.g. event).
 * Apply only one query, but add dummy annotations to get all desired results: one can create relations to the page N.A. ("not available") or similar, or assign an attribute value of the correct type denoting N.A. For type string this can be "N.A." or similar. For other types such as integer, float and date some extreme value can be chosen, but it makes results less easy to understand, the reader has to know (or be told) the convention that the special value denotes "not available"; even for people who know the convention the results become less readable. Instead of "N.A." we may choose a pagename/value which is alphabetically at the start or end, e.g. "! N.A." at the start or "~ N.A." at the end (though before various special characters), to position the results as desired.
 * In the case of an attribute with an extreme value for "N.A.", use two queries: one with an inequality to exclude the pages with the extreme value, and one selecting the pages with the extreme value, but not displaying this value. The results are now not cluttered with the extreme value, but the factbox of the page for which it is defined still shows it, with still the problem of making it less understandable and readable.

Javascript sorting
Tables created by Semantic MediaWiki are connected by the delivered stylesheet to a Javascript program that allows the user to sort, inside the browser, the rows of the displayed table by the content of a specified column, either ascending or descending. This overrides the order of the rows as specified in the query, but leaves intact the order of multiple items in a cell (ascending or descending, as specified by the sort parameter).

First population table on this page:

Numeric values without grouping character are numerically sorted. However, "12,345" is sorted based on "12", so for ascending order it comes before 13.

Second and third population table on this page:

Sorting is alphabetical, with the comma before the digits, for ascending order 1,795,000 comes before 143,346.