Dataview: How to specify the order of properties

What I’m trying to do

I would like to specify the order status properties for the GROUP BY dataview query:

TABLE rows.file.link AS Name
FROM #Book
WHERE !contains(file.path, "Template")
SORT dateFinished ASC
GROUP BY status

I would like this to be ordered: [reading, unread, read], but currently gives:

Things I have tried

I know you can sort by key ASC/DESC, but this does not give the required result:

TABLE rows.file.link AS Name
FROM #Book
WHERE !contains(file.path, "Template")
SORT dateFinished ASC
GROUP BY status
SORT key DESC

The trick to such a request is to assign a number to the property, and then sort on that number instead of the actual property value. This can be achieved using a combination of FLATTEN and object(). Where object() creates the mapping from a status to a number, and the FLATTEN is used to store this into a new variable.

We can then group on this number, but we don’t really want to see that number, so a little trickery with TABLE WITHOUT ID is needed. Doing all of this we get a query like the following (where I’m using file.folder/file to limit my test set, adapt to your use case):

```dataview
TABLE WITHOUT ID
  min(rows.status) as Status,
  rows.file.link as Books
FLATTEN object(
 "reading", 1,
 "unread", 2,
 "read", 3)[status] as statusNo
 WHERE file.folder = this.file.folder 
  AND file != this.file
 GROUP BY statusNo
 ```

Which yields this result in my test scenario:
image