Jump to: navigation, search

Sorting by default

Query results are typically pages, subobjects and/or values of datatype "Page"[1] If no specific sorting condition is provided, then these are sorted alphabetically, in ascending order.

The alphabetical sorting order, or collation to be precise, is dictated by MediaWiki's $wgCategoryCollation setting, if this is set to uppercase or identity. However, Semantic MediaWiki does not yet support uca-default or any of its language-specific adjustments (#1383).

Natural sorting of pages may not be supported out of the box. An example is the alphanumeric kind by which numeric portions of a page title are treated as numbers - e.g. so that b9 comes before rather than after b1000.[2] As a possible halfway solution, note that the sortkey of a page may be overridden using the {{DEFAULTSORT}} magic word.

Names of subobjects usually consist of the title of the page on which they are declared, followed by a hashtag (#) and a custom identifier or autogenerated series of numbers (hash-numbering). Beginning with SMW 2.0, subobjects may also be assigned a custom sortkey: see Help:Adding subobjects#Specifying a sortkey.

Providing sorting conditions: using parameters

The following query parameters let you specify sorting conditions for a query.

Parameter Possible values Description
  1. a property name (without namespace prefix)
  2. a property chain
  3. a category name (since SMW 2.5 - see here for a demonstration).
  4. blank, which is interpreted as main result column (default behaviour)
The name of the property, property chain or category to be used for sorting queries. A comma-separated list of values is possible to allow for secondary (and tertiary, etc.) sorting orders. The values provided for order will then follow the same sequence.


ascending (short: asc)
descending (short: desc) or reverse
random (short: rand)
or a comma-separated list of those if more than one property is used for sorting

Defines whether results should be ordered in ascending, descending or random order. Default: ascending.
offset number where to start

The sort parameter takes a property name or a comma-separated list of property names. It lets you sort results by the order in which the values of any selected property are "naturally" sorted. By "natural" sorting is meant that:

  • sorting by a property of datatype "page" is alphabetical (the note above about $wgCategoryCollation and DEFAULTSORT also applies here)
  • sorting by a property of datatype "text" is alphabetical
  • sorting by a property of datatype "number" is numerical
  • sorting by a property of datatype "date" is chronological.
and so on...

(Use of < and > comparators in queries follow the same "natural" sorting order)


You have pages about cities and you want to present a list of cities sorted by population size. The sort parameter would then take the property that is used for storing population size. If this list is to be shown in reverse order of population size, from the city that has the largest population downwards, the order parameter would be set to descending. Such a query would look like:

{{#ask: [[Category:City]]
 |?Population size
 |sort=Population size

Providing multiple sorting conditions

It is possible to provide more than one sorting condition. If multiple results turn out to be equal regarding the first sorting condition, the next condition is used to order them and so on.


We want to get a list of cities by their average number of rainy days per year, but grouped by the country they are in, with the following query:

{{#ask: [[Category:City]]
 |?Located in=Country
 |?Average rainy days
 |sort=Located in,Average rainy days
resulting in
 CountryAverage rainy days
San DiegoCalifornia
United States of America
BellevilleUnited States of America39

Restrictive implications of using sort

Sorting a query also influences the result of a query, because it is only possible to sort by property values that a page actually has. Therefore, if a query is ordered by a property (say Population) then SMW will usually restrict the query results to those pages that have at least one value for this property (i.e. only pages with specified population appear). Therefore, if the query does not require yet that the property is present in each query result, then SMW will silently add this condition.

What if there are multiple property values per page?

If a property that is used for sorting has more than one value per page, then this page will still appear only once in the result list. The position that the page takes in this case is not defined by SMW and may correspond to either of the property values. In the above examples, this would occur if one city has multiple population numbers specified, or if one city is located in multiple countries each of which has a population. It may be best to avoid such situations.

Providing sorting conditions: alternatives

Sortable tables

Query results displayed in a result table can also be ordered dynamically by clicking on the small sort icons found in the table heading of each column. This function requires JavaScript to be enabled in the browser and will sort only the displayed results. So if, e.g., a query has retrieved the twenty world-largest cities by population, it is possible to sort these twenty cities alphabetically or in reverse order of population, but the query will certainly not show the twenty world-smallest cities when reversing the order of the population column. The dynamic sorting of tables attempts to use the same order as used in SMW queries, and in particular orders numbers and dates in a natural way. However, the alphabetical order of strings and page names may slightly vary from the wiki's alphabetic order, simply because there are many international alphabets that can be ordered in different ways depending on the language preference.


The special page "Ask" has a simple interface to add one or more sorting conditions to a query. The name of the property to sort by is entered into a text input, and ascending or descending order can be selected.


  1. The latter may be true if an inverse property is used, even if the page does not exist.
  2. See Bugzilla, e.g. #T8948

See also