Skip to content

Commit f6bc00d

Browse files
committed
docs: Document 'sort by function'.
1 parent 3d7673a commit f6bc00d

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

docs/Queries/Sorting.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ publish: true
1313
This page is long. Here are some links to the main sections:
1414

1515
- [[#Default sort order]]
16+
- [[#Custom Sorting]]
1617
- [[#Sort by Task Statuses]]
1718
- [[#Sort by Dates in Tasks]]
1819
- [[#Sort by Other Task Properties]]
@@ -51,7 +52,16 @@ However, any `sort by` instructions in queries take precedence over these defaul
5152
5253
## Custom Sorting
5354

54-
==TODO Populate this section==
55+
> [!released]
56+
> `sort by function` was introduced in Tasks X.Y.Z.
57+
58+
Tasks provides many built-in sorting options, but sometimes they don't quite do what is wanted by all users.
59+
60+
Now Tasks has a powerful mechanism for you to create your own **custom sort orders**, offering incredible flexibility.
61+
62+
There are many examples of the custom filtering instruction `sort by function` in the documentation below, with explanations, for when the instructions built in to Tasks do not satisfy your preferences.
63+
64+
You can find out more about this very powerful facility in [[Custom Sorting]].
5565

5666
## Sort by Task Statuses
5767

@@ -61,6 +71,9 @@ For more information, including adding your own customised statuses, see [[Statu
6171

6272
- `sort by status` (done or todo)
6373

74+
> [!Tip]
75+
> `sort by status.type` gives a much more useful sort order than `sort by status`. See [[#Status Type#]] below.
76+
6477
Since Tasks X.Y.Z, **[[Custom Sorting|custom sorting]] by status** is now possible.
6578

6679
<!-- placeholder to force blank line before included text --><!-- include: CustomSortingExamples.test.other_properties_task.isDone_docs.approved.md -->

docs/Scripting/Custom Sorting.md

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ publish: true
1313

1414
- Define your own custom task sort order, using JavaScript expressions such as:
1515
- `sort by function task.description.length`
16+
- We call the result of this expression a **sort key**.
17+
- Tasks with **lower values** from the sort key expression are sorted **before** tasks with **higher expression** values.
1618
- There are examples in [[Sorting]].
1719
- Search for `sort by function` in that file.
1820
- Find all the **supported tasks properties** in [[Task Properties]] and [[Quick Reference]].
@@ -48,15 +50,17 @@ The instructions look like this:
4850
- `sort by function <expression>`
4951
- `sort by function reverse <expression>`
5052

51-
The expression is evaluated (calculated) on each task that matches your query, and the expression result is used as the sort key for the task.
53+
The expression is evaluated (calculated) on each task that matches your query, and the expression result is used as the **sort key** for the task.
5254

53-
==TODO Update this==
55+
This table shows how the supported sort key value types behave.
5456

55-
| Desired heading | Values that you can return |
56-
| --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
57-
| A single sort name for the task | A single value, such as `'sort name'`.<br>An array, with a single value in, such as `['sort name']`. |
58-
| Display the task potentially more than once (as is done by `sort by tags`) | An array of values, such as:<br>`['heading 1', 'heading 2']` |
59-
| No heading | `null`<br>Empty string `''`<br>Empty array `[]` |
57+
| Sort key expression types | Values that you can return |
58+
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
59+
| Strings | Example: `sort by function task.originalMarkdown`<br>Tasks are sorted alphabetically by the string key values. The sort is case-sensitive. It is aware of numbers and sorts them logically. |
60+
| Numbers | Example: `sort by function task.description.length`<br>Numbers are sorted in ascending order. The lower the number, the earlier the task is sorted. |
61+
| Boolean values: `true` and `false` | Example: `sort by function task.status.name.includes('!!')` <br>Tasks with sort key `true` sort before ones with `false`. |
62+
| `TasksDate` and `Moment` objects | Example: `sort by function task.created`<br>See [[Sorting#How dates are sorted\|How dates are sorted]]. |
63+
| `null` | `null` sorts after valid `TasksDate` and `Moment` objects, and before all other sort key values. |
6064

6165
The `expression` can:
6266

@@ -65,19 +69,15 @@ The `expression` can:
6569

6670
The `expression` must:
6771

68-
==TODO Update this==
69-
70-
- use properties on a given task, such as `task.description`, `task.status.name`
71-
- See the reference page [[Task Properties]] for all the available properties
72-
- return one of:
73-
- either a single value of any type that can be converted to string
74-
- or an array of values (in which case, the task will be displayed multiple times, once under each heading generated from the array)
72+
- use properties on a given task, such as `task.description`, `task.status.name`.
73+
- See the reference page [[Task Properties]] for all the available properties.
74+
- return one of types of values listed in the table above.
7575

7676
## Example custom sorts
7777

7878
Below are some examples to give a flavour of what can be done with custom sorts.
7979

80-
You can find many more examples by searching for `sort by function` in the [[Sorting]] page.
80+
You can find some more examples by searching for `sort by function` in the [[Sorting]] page.
8181

8282
### Text property examples
8383

@@ -109,6 +109,11 @@ sort by function \
109109

110110
### Date property examples
111111

112+
Useful sections:
113+
114+
- [[Task Properties#Values in TasksDate Properties|Values in TasksDate Properties]]
115+
- [[Sorting#How dates are sorted|How dates are sorted]]
116+
112117
<!-- placeholder to force blank line before included text --><!-- include: CustomSortingExamples.test.dates_task.due_docs.approved.md -->
113118

114119
```javascript
@@ -134,6 +139,10 @@ sort by function reverse task.urgency
134139

135140
### File property examples
136141

142+
Useful sections:
143+
144+
- [[Task Properties#Values for File Properties|Values for File Properties]]
145+
137146
<!-- placeholder to force blank line before included text --><!-- include: CustomSortingExamples.test.file_properties_task.file.folder_docs.approved.md -->
138147

139148
```javascript
@@ -153,28 +162,11 @@ sort by function task.file.path === query.file.path
153162

154163
## Troubleshooting
155164

156-
> [!Warning]
157-
> Currently most types of error in function expressions are only detected when the search runs.
158-
>
159-
> This means that error messages are displayed in the sort headings, when results are viewed.
165+
> [!Tip]
166+
> To test the values obtained from your sort key, replace `sort by` with `group by`. The generated group headings provide a sort of "debugger" to show expression values.
160167
>
161-
> In a future release, we plan to show errors in formulae when the query block is being read.
168+
> However, be aware that whilst `group by` expressions can return arrays of values, `sort by` does not yet support arrays.
162169

163-
### Syntax error
164-
165-
The following example gives an error:
166-
167-
````text
168-
```tasks
169-
sort by function hello
170-
```
171-
````
172-
173-
gives this heading name:
174-
175-
```text
176-
##### Error: Failed calculating expression "hello". The error message was: hello is not defined
177-
```
170+
## Limitations of Custom Sorting
178171

179-
> [!todo]
180-
> Do syntax-error checking when parsing the instruction
172+
- Arrays cannot yet be used as sort keys, as we have not figured out the appropriate way to sort arrays of different lengths.

docs/Support and Help/Known Limitations.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ This page gathers together all the documentation on known limitations of the plu
7171

7272
![[Regular Expressions#Known limitations]]
7373

74+
## Queries: Custom Sorting
75+
76+
![[Custom Sorting#Limitations of Custom Sorting]]
77+
7478
## Settings: Status Settings
7579

7680
![[Status Settings#Limitations and Issues]]

0 commit comments

Comments
 (0)