Skip to content

Commit 75195f3

Browse files
authored
Merge pull request #2604 from obsidian-tasks-group/docs-custom-sort
docs: Document custom sorting and `sort by function`
2 parents c45d089 + fefe105 commit 75195f3

File tree

9 files changed

+59
-53
lines changed

9 files changed

+59
-53
lines changed

docs/Queries/About Queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ The only exceptions to this flexibility are:
8787

8888
- When [[Combining Filters]], the boolean operators such as `AND`, `OR` and `NOT` must still be capitalised.
8989
- In [[Regular Expressions]], the search pattern and flags are still case-sensitive.
90-
- The code in expressions in [[Custom Filters]] and [[Custom Grouping]] remain case-sensitive.
90+
- The code in expressions in [[Custom Filters]], [[Custom Sorting]] and [[Custom Grouping]] remain case-sensitive.
9191

9292
### Why is my query not working?
9393

docs/Queries/Line Continuations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Explanation of this Tasks code block query:
4242
```
4343
<!-- endSnippet -->
4444

45-
This facility will be helpful for long [[Combining Filters]], [[Custom Filters]], and [[Custom Grouping]] lines, and other queries that may be difficult to read on one line.
45+
This facility will be helpful for long [[Combining Filters]], [[Custom Sorting]], and [[Custom Grouping]] lines, and other queries that may be difficult to read on one line.
4646

4747
There are some more realistic examples towards the end of the [[Grouping#Due Date|Due date custom grouping examples]].
4848

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 Grouping.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ The available task properties are also shown in the [[Quick Reference]] table.
4040

4141
### Available Query Properties
4242

43-
The Reference section [[Query Properties]] shows all the query properties available for use via [[Placeholders]] in custom grouping.
44-
45-
Any placeholders in custom groups must be surrounded by quotes.
43+
The Reference section [[Query Properties]] shows all the query properties available for in custom grouping.
4644

4745
> [!released]
48-
> Query properties and placeholders were introduced in Tasks 4.7.0.
46+
>
47+
> - Query properties and placeholders were introduced in Tasks 4.7.0.
48+
> - Direct access to Query properties was introduced in Tasks 5.1.0.
4949
5050
### Expressions
5151

docs/Scripting/Custom Sorting.md

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ 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]].
1921
- A number of properties are only available for custom sorting, and not for built-in sorting instructions.
2022
- Find all the **supported query properties** in [[Query Properties]].
21-
- ==TODO Query properties are not yet supported in custom sorters.==
2223
- Learn a bit about how expressions work in [[Expressions]].
2324

2425
## Custom sorting introduction
@@ -40,12 +41,7 @@ The Reference section [[Task Properties]] shows all the task properties availabl
4041
The available task properties are also shown in the [[Quick Reference]] table.
4142
### Available Query Properties
4243

43-
The Reference section [[Query Properties]] shows all the query properties available for use via [[Placeholders]] in custom sorting.
44-
45-
Any placeholders in custom sorts must be surrounded by quotes.
46-
47-
> [!released]
48-
> Query properties and placeholders were introduced in Tasks 4.7.0.
44+
The Reference section [[Query Properties]] shows all the query properties available for use in custom sorting.
4945

5046
### Expressions
5147

@@ -54,15 +50,17 @@ The instructions look like this:
5450
- `sort by function <expression>`
5551
- `sort by function reverse <expression>`
5652

57-
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.
5854

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

61-
| Desired heading | Values that you can return |
62-
| --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
63-
| 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']`. |
64-
| 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']` |
65-
| 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. |
6664

6765
The `expression` can:
6866

@@ -71,19 +69,15 @@ The `expression` can:
7169

7270
The `expression` must:
7371

74-
==TODO Update this==
75-
76-
- use properties on a given task, such as `task.description`, `task.status.name`
77-
- See the reference page [[Task Properties]] for all the available properties
78-
- return one of:
79-
- either a single value of any type that can be converted to string
80-
- 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.
8175

8276
## Example custom sorts
8377

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

86-
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.
8781

8882
### Text property examples
8983

@@ -115,6 +109,11 @@ sort by function \
115109

116110
### Date property examples
117111

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+
118117
<!-- placeholder to force blank line before included text --><!-- include: CustomSortingExamples.test.dates_task.due_docs.approved.md -->
119118

120119
```javascript
@@ -140,6 +139,10 @@ sort by function reverse task.urgency
140139

141140
### File property examples
142141

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

145148
```javascript
@@ -159,28 +162,11 @@ sort by function task.file.path === query.file.path
159162

160163
## Troubleshooting
161164

162-
> [!Warning]
163-
> Currently most types of error in function expressions are only detected when the search runs.
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.
164167
>
165-
> This means that error messages are displayed in the sort headings, when results are viewed.
166-
>
167-
> In a future release, we plan to show errors in formulae when the query block is being read.
168-
169-
### Syntax error
168+
> However, be aware that whilst `group by` expressions can return arrays of values, `sort by` does not yet support arrays.
170169

171-
The following example gives an error:
172-
173-
````text
174-
```tasks
175-
sort by function hello
176-
```
177-
````
178-
179-
gives this heading name:
180-
181-
```text
182-
##### Error: Failed calculating expression "hello". The error message was: hello is not defined
183-
```
170+
## Limitations of Custom Sorting
184171

185-
> [!todo]
186-
> 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/Scripting/Expressions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ publish: true
2222
- The expression is a string instruction.
2323
- As of Tasks 4.4.0, variables, functions, `if` blocks and similar can be used. See [[#More complex expressions]].
2424
- As of Tasks 5.0.0, long expressions can be written over multiple lines thanks to [[Line Continuations]].
25-
- Depending on the context, one or two tasks are passed in to the expression, and a calculation is performed.
26-
- As of Tasks 4.0.0, in fact only a single task is passed in, to implement [[Custom Grouping]].
25+
- One task is passed in to the expression, and a calculation is performed.
26+
- As of Tasks 4.0.0, a single task is passed in, to implement [[Custom Grouping]].
2727
- As of Tasks 4.2.0, a single task is passed in, to implement [[Custom Filters]].
28+
- As of Tasks X.Y.Z, a single task is passed in, to implement [[Custom Sorting]].
2829
- Tasks then calculates a value from the inputs.
2930

3031
## Example expressions

docs/Scripting/Query Properties.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ In a growing number of locations, Tasks allows programmatic/scripting access to
1717

1818
- [[Placeholders]]
1919
- [[Custom Filters]]
20+
- [[Custom Sorting]]
2021
- [[Custom Grouping]]
2122

2223
This page documents all the available pieces of information in Queries that you can access.

docs/Scripting/Task Properties.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ publish: true
1414
In a growing number of locations, Tasks allows programmatic/scripting access to values in your Tasks:
1515

1616
- [[Grouping#Custom Groups]]
17+
- [[Sorting#Custom Sorting]]
1718
- [[Filters#Custom Filters]]
1819

1920
This page documents all the available pieces of information in Tasks that you can access.

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)