Specify the sort order in Dataview Query

I am using a Dataview query to display all the notes which have a YAML field called “area”, and the value of that field is a link to the current note.
I am then grouping those notes using the “realm” field.
Right now, the groups are sorted in alphabetical order.
I would like them to be sorted in a custom order.

    dataview
    TABLE
        rows.file.link AS "Name",
        rows.type AS "Type"
    FROM -"Templates"
    WHERE contains(area, this.file.link)
    SORT file.name ASC
    GROUP BY realm

I tried converting the query to DataviewJS, but couldn’t figure out the WHERE clause.

Since this is basically a DB query, you’d have to create some field with your custom sort order.

The SORT clause. Is that sorting the order of the GROUPS or the order of the individual notes within the GROUPS?

Hi.
In DQL queries you need to “construct” the wanted sort manually.
For example:

Wanted order: Literature, Math, Languages, Drawing, Tech, Personal

DV command: use function `choice()` to define the wanted order

Querie:

```dataview
TABLE
    rows.file.link AS "Name",
    rows.type AS "Type"
FROM -"Templates"
WHERE contains(area, this.file.link)
SORT file.name ASC
GROUP BY realm
SORT choice(realm = "Literature", "1",
choice(realm = "Math", "2",
choice(realm = "Languages", "3",
choice(realm = "Drawing", "4",
choice(realm = "Tech", "5",
choice(realm = "Personal", "6", "other"))))))
```
8 Likes

That worked perfectly. Thanks for the help. :smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.