DataArray: sorting by multiple keys #364
-
Given that the query language already allows to express e.g. Here's the relevant line from the API: sort<U>(key: ArrayFunc<T, U>, direction?: 'asc' | 'desc', comparator?: ArrayComparator<U>): DataArray<T>; |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 2 replies
-
You can sort on an array - |
Beta Was this translation helpful? Give feedback.
-
@blacksmithgu, thank you for your feedback, and apologizes for the late reply. Unfortunately, using this syntax didn't produce the expected ordering here (latest Dataview & Obsidian versions on Ubuntu 21.04). Here's my query (in French) let pages = dv.pages("[[Projet]]");
dv.table([
"Tâche", "Priorité", "Projets", "Revue"
],
pages
.where( p =>
(!p.Responsable || p.Responsable.path=="Moi")
&& p.Statut == "#État/Projet/1_Actif"
&& p["Date_de_revue"]
)
.map( p => [
p.file.link, p.Priorité, p.Projets, p.Date_de_revue
])
.sort(p => [p.Date_de_revue, p.Priorité])
) and here's the relevant part of the output: Notice that the priority (2nd key) is the same, but the dates (1st key) are sorted anyhow. |
Beta Was this translation helpful? Give feedback.
-
Sort before you do the map; after the map, the data is a collection of arrays instead of the original page objects:
|
Beta Was this translation helpful? Give feedback.
-
Indeed, it's working just fine. Thanks again ! let pages = dv.pages("[[Project]]");
dv.table([
"Task", "Priority", "Projects", "Review date"
],
pages
.where( p =>
p.Status == "#Status/1_Active"
&& p["review-date"]
)
.sort(p => [p["review-date"], p.Priority])
.map( p => [
p.file.link, p.Priority, p.Projects, p["review-date"]
])
) |
Beta Was this translation helpful? Give feedback.
-
The existing documentation already has an example of using That said, I don't think it is wise to clutter dataview's documentation with how JS programming works. One will need to learn some JS to use dataviewjs, and there are plenty of JS documentation explaining how function return and the dot operator works. They can also ask questions in the discussion section, Obsidian forum/discord, and stackoverflow too. |
Beta Was this translation helpful? Give feedback.
-
I had a similar issue where a grouped list wouldn’t sort the entries at the 2nd level. But as @blacksmithgu said, you only have to know where to sort what 😉 Since the world can always use examples, here’s a small
Ingredients are stored in the folder "Recipes/Ingredients" and have
|
Beta Was this translation helpful? Give feedback.
-
Better UX for being able to specify per-key sort direction is probably still useful, though I'm not sure if I should make it a different method
|
Beta Was this translation helpful? Give feedback.
-
Alternatively, perhaps the sort direction could also be an array?
That seems like a better approach. |
Beta Was this translation helpful? Give feedback.
Sort before you do the map; after the map, the data is a collection of arrays instead of the original page objects: