PUBLISHED : {par ...}

**SPIP**

# Article validated

The article "** {par ...}** (https://www.spip.net/en_article6376.html)" was
validated by <multi>[ar]جورج[fr]George [en]George</multi>.

* * *

## {par ...}

Saturday 10 October 2020 , by [jack](.././?page=auteur&id_auteur=593&) ,
[tcharlss](.././?page=auteur&id_auteur=6944&)

The **`{par ...}`** criterion defines the order in which a loop displays
the items.

## Table of contents

* Sort by number
* Sort by language and alphabetical order
* Sorting using several field
* Sort dynamically
* Random order
* Order the elements of a search by relevance

The **`{par ...}`** criterion defines the order in which a loop displays
the items. For instance, articles can be sorted by their date of online
publication: `{par date}`, their date of "earlier publishing": `{par
date_redac}`, by their title: `{par titre}`, and so on.

## Sort by number[|Back to the table of contents](#s-Sort-by-number)

It is possible to sort the items by the numerical prefix of one of the
item’s *fields* (titre (the title), surtitre (the top title), ps (the
postscript). [1] . The criterion syntax is as follow :
**`{par num `***`field`***`}`**.
For instance, to sort according to the title numerical prefix, `{par num
titre}` will display the item in the ascending order of their titles
numerical prefix, starting, if any, by the prefix-less titles, which
won’t be sorted.

**Note :**

1. the `{par num ...}` criteria takes into account all the values starting
by a number ; thus `{par num surtitre}` will place the top title «`14 july
1789`» between «`10. Something`» et «`20. Something else`».
2. when the prefixes start by `` (zero), `{par num titre}` will sort the
prefixes including the greater amount of zeros prior to the prefixes with
the lesser amount : `00000. Title…`, `0000. Title…`, `000. Title…`,
`0. Title…`

## Sort by language and alphabetical order[|Back to the table of
contents](#s-Sort-by-language-and-alphabetical-order)

In a multilingual website, the `{par multi ...}` criterion allow you to
sort alphabetically in each language separately ; for example: `{par multi
titre}`. Without *multi*, the items would be sorted with all the languages
mixed.

## Sorting using several field[|Back to the table of
contents](#s-Sorting-using-several-field)

It is possible to sort item using several fields. `{par field1, field2}`
indicates the sorting fields by priority: items will be sorted according to
field1, than according to field2 when field1 have identical values. It is
possible to indicate as many fields as necessary.
For instance `{par date, titre}` will sort the items by date (from the
oldest to the most recent), and items having the same date will be sorted
by their title (in alphabetical order).

It is also possible to use several criteria `{par ...}` in a loop.
For example: `{par date} {par titre}` is equivalent to `{par date, titre}`.

**Warning:**
When several sorting criteria are used, the criterion `{inverse}`
(https://www.spip.net/ecrire/?exec=article&id_article=6616) applies only to
the previous sorting criterion. Thus `{par date} {par titre} {inverse}`
will apply the reverse sorting **only** on the field `titre` (*from Z to
A*), the `date` keeping its default sorting (*from oldest to newest*)

This is why we introduced the syntax **`{!par ...}`** which inverts *a*
particular sorting criterion. For example: `{!par date} {par num titre}`
sorts the results by descending date (*from the most recent to the oldest*)
then by ascending number for titles with the same date.
However: `{!par date, num titre}` sorts the results by descending date
(*from the most recent to the oldest*) then by descending number for titles
with the same date; this syntax is therefore equivalent to `{! par date}
{!par num titre}` but is **different** from `{par date, num titre}
{inverse}` (where `{inverse}` applies **only** on `date`).

So be careful to choose your criteria syntax according to the results you
expect.

## Sort dynamically[|Back to the table of contents](#s-Sort-dynamically)

The criterion `{par ...}` also accepts dynamically specified SQL field
names, such as `#ENV`. For example: `{par #ENV{variable}}` will sort on the
value of "*variable*" passed in the url *`&variable=titre`* provided that
this value is a SQL field of the table concerned by the loop.

So `&variable=num titre` will not work since no SQL field is named `num
titre`. In this case it is better to use criterion `{tri}`
(https://www.spip.net/ecrire/?exec=article&id_article=6588).

## Random order[|Back to the table of contents](#s-Random-order)

It is possible to obtain a list sorted in a random order by using the
criterion `{par hasard}`.

## Order the elements of a search by relevance[|Back to the table of
contents](#s-Order-the-elements-of-a-search-by-relevance)

In order to classify the results of a search form
(https://www.spip.net/ecrire/?exec=article&id_article=2218) by relevance,
we will use the criterion `{par points} {inverse}` ; a simple `{par points}
` ordering the results from the smallest to the largest relevance.

**P.S.**
See also the `{inverse}`
(https://www.spip.net/ecrire/?exec=article&id_article=6616) criteria

[1] The numerical prefix syntax is as follow :
a number, followed by a dot or a closing parenthesis, followed by a space.

*examples :
` 015. `A title
` 51) `Another title*

— Sent by SPIP (https://www.spip.net/)

rubon199-7e493.jpg