Help:Sorting

Semantic MediaWiki will usually attempt to sort results by the natural order that the values of the selected property may have:
 * "numbers" are sorted numerically,
 * "text" is sorted alphabetically and
 * "dates" are sorted chronologically.

The order therefore is the same as in the case of the  and   comparators in queries. If no specific sorting condition is provided, results will be ordered by their page name.

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. For example we could 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:

resulting in

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 (See also the info box below). 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. But SMW will always try to find the ordering property within the given query first, and it is even possible to order query results by subproperties. Some examples should illustrate this:


 * ordered by "Population" will present the cities with population in ascending order. The query result is the same as without the sorting.


 * ordered by "Population" will again present the cities with population in ascending order. The query result may be modified due to the sorting condition: if there are cities without a population provided, then these will no longer appear in the result.


 * ordered by "Population" will present the cities ordered by the population of the countries they are located in. The query result is not changed, but "population" now refers to a property used in a subquery. Again the population must be annotated for the countries to avoid it being omitted due to the sorting condition.

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.

Sorting by default
Query results are typically pages, subobjects and/or values of datatype "Page" 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. Semantic MediaWiki supports uca-default and of its language-specific adjustments since version 3.0.0 (#1383). For details see Help:$smwgEntityCollation.

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. As a possible halfway solution, note that the sortkey of a page may be overridden using the  magic word. If you add a sortkey using  you do not need to specify extra sorting in your query since Semantic MediaWiki will adhere to the values thus set.CiteRef::mail:user:smw:tc5075716

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.

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

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 is alphabetical (the note above about  and the   variable also applies here)CiteRef::sb:smw:defaultsort
 * sorting by a property of is alphabetical
 * sorting by a property of is numerical
 * sorting by a property of is chronological.
 * and so on...

(Use of 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:
 * Example

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:
 * Example:

resulting in

Restrictive implications of using sort
Sorting a query also influences the result of a query, because, by default, 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</tt>) 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. See the option  of  to change that behaviour.CiteRef::gh:smw:2823

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.

Sorting the main column
If sorting should include the mainlabel and properties at the same time, you can treat the mainlabel as empty string (as there is no the identifier for it). So this works, it sorts first after the mainlabel and second after the Average rainy days

resulting in

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.

Demo
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.