Help:Sorting

From semantic-mediawiki.org
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
sort a property name or a comma-separated list of property names (without namespace prefixes). name of property or properties to use for sorting queries

Starting from SMW 2.4 (yet to be released), you can also sort by category - see here for a sneak preview.

order

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)

Example

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
 |order=descending
}}

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.

Example

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
 |order=asc,desc
}}
resulting in
 CountryAverage rainy days
SydneyAustralia143.7
KarlsruheBaden-Württemberg124
San DiegoCalifornia
United States of America
29.4
LondonEngland145
ParisFrance111.5
MunichGermany129.4
StuttgartGermany
Baden-Württemberg
112.4
FrankfurtGermany108.9
BerlinGermany106.3
RomeItaly84.5
Warsaw159
AmsterdamNetherlands234
PortoPortugal140
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.

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.

Notes

  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