Sorting Dataview JS

I had this code which would sort my dataview query showing the most recently created file at the top.

But for some reason it no longer works. I have even tried changing “desc” to “asc” to no avail

Is there a better way that I can sort with Dataview JS?

I don’t think you should be using sort(dv.pages.cdate, "date") to sort the list, because dv.pages.cdate isn’t a valid sort criterion. Perhaps you mean something like?:

sort(page => page.file.cday, "desc")

Here’s a working example from my vault:

```dataviewjs
dv.list(dv.pages()
	.where(page => page.file.name.startsWith("A"))
	.sort(page => page.file.cday, "desc")
	.file.cday)
```

Hi Craig, thanks so much for your reply.

I tested your solution and it worked to sort the pages correctly.

```dataviewjs
dv.list(dv.pages('"reviews/daily/2022"')
.where(page => page.file.name != "2022-11-27" && page.file.ctime >= dv.date("2022-11-20"))
.sort(page => page.file.cday, "desc") .file.cday)

Produced:

However it seems the big issue is with the ‘taskList’ part because when I changed the code to show tasks:

```dataviewjs
dv.taskList(dv.pages('"reviews/daily/2022"')
.where(page => page.file.name != "2022-11-27" && page.file.ctime >= dv.date("2022-11-20"))
.tasks.where(t => !t.completed)
.sort(page => page.file.cday, "desc") .file.cday)

It just shows this:

Any idea what’s causing this?

Topic

Summary
  • How to list task items in the descending order where the files were created?

Test

Summary
  • dataview: v0.5.46

Input

Summary

dictionary files:

  • Location: “100_Project/01_dataviewjs/01_by_example/Q81_TasksSort/Q81_test_data”

folder: 03

  • filename : dic_19750301
  • file.cday : “2022-11-24”
---
Date: 1975-03-01
---
#Project/P03

- [[Note P]] main task GH
  - [ ] task g
  - [ ] task h
      - [ ] task h1
      - [ ] task h2



folder: 04

  • filename : dic_19750401
  • file.cday : “2022-11-25”
---
Date: 1975-04-01
---
#Project/P04

- [ ] [[Note P]] main task IJ
  - [ ] task i
  - [ ] task j
      - [ ] task j1
      - [ ] task j2



folder: 07_done

  • filename : dic_19750701
  • file.cday : “2022-11-28”
---
Date: 1975-07-01
---
#Project/P07

### main tasks : CASE S1
- [[Note P]] main task GH
  - [x] task g
  - [x] task h
      - [x] task h1
      - [x] task h2



folder: 08_empty

  • filename : dic_19750801
  • file.cday : “2022-11-29”
---
Date: 1975-08-01
---
#Project/P08

## input
### Summary



DQL10_use_fTasks_filter_by_uncompleted_tasks_and_TASK

Summary

Main DQL

Code Name Data type Group By Purposes Remark
DQL10
_use_fTasks
_filter_by
_uncompleted_tasks
_and_TASK
flattened file.tasks yes 1.To filter by file.name
2.To filter by file.ctime
3.To filter by an uncompleted task
4.To group by header
5.To sort by rows.file.cday in descending order
6.To display the result as a taskList [with the desired structure]

Code DQL10_use_fTasks_filter_by_uncompleted_tasks_and_TASK

Summary_code
title: DQL10_use_fTasks_filter_by_uncompleted_tasks_and_TASK =>1.To filter by file.name 2.To filter by file.ctime 3.To filter by an uncompleted task 4.To group by header 5.To sort by rows.file.cday in descending order 6.To display the result as a taskList [with the desired structure]
collapse: close
icon: 
color: 
```dataview
TASK
FROM "100_Project/01_dataviewjs/01_by_example/Q81_TasksSort/Q81_test_data"

WHERE file.name != "2022-11-27"
WHERE file.ctime >= date("2022-11-20")

WHERE !completed

GROUP BY header
SORT rows.file.cday DESC

```

Screenshots(DQL10)


DVJS10_use_fTasks_filter_by_uncompleted_tasks_and_taskList

Summary

Main DVJS

Code Name Data type Group By Purposes Remark
DVJS10
_use_fTasks
_filter_by
_uncompleted_tasks
_and_taskList
file.tasks yes 1.To filter by page.file.name and page.file.ctime
2.To filter by an uncompleted task
3.To group by k.header
4.To sort by file.cday in descending order
5.To display the result as a taskList [with the desired structure]

Notes

Summary

Q1: What is the same code as the DVJS10?

Summary_Q1

A1:

Another Example11
title: Another Example11 => 
collapse: close
icon: 
color: 
```dataviewjs
dv.taskList(
    dv
        .pages(
            '"100_Project/01_dataviewjs/01_by_example/Q81_TasksSort/Q81_test_data"'
        )
        .where(
            (page) =>
                page.file.name !== "2022-11-27" &&
                page.file.ctime >= dv.date("2022-11-20")
        )
        .file.tasks.where((t) => !t.completed)
        .groupBy((k) => k.header)
        .sort((k) => dv.page(k.key.path).file.cday, "desc")
);
```

code DVJS10_use_fTasks_filter_by_uncompleted_tasks_and_taskList

Summary_code
title: DVJS10_use_fTasks_filter_by_uncompleted_tasks_and_taskList => 1.To filter by page.file.name and page.file.ctime 2.To filter by an uncompleted task 3.To group by k.header 4.To sort by file.cday in descending order 5.To display the result as a taskList [with the desired structure]
collapse: close
icon: 
color: 
```dataviewjs
// M11. define pages : gather all relevant Tasks
// #####################################################################
let pages = dv
    .pages('"100_Project/01_dataviewjs/01_by_example/Q81_TasksSort/Q81_test_data"')
    .where(
        (page) =>
            page.file.name !== "2022-11-27" &&
            page.file.ctime >= dv.date("2022-11-20")
    );
    //.sort((page) => page.file.cday, "desc");


// M21. define myTasks : gather all relevant Tasks
// #####################################################################
let myTasks = pages.file.tasks
    .where((t) => !t.completed)
    .groupBy((k) => k.header)
    .sort((k) => dv.page(k.key.path).file.cday, "desc");


// M31. output myTasks : 
// #####################################################################
dv.taskList(myTasks);


```

Screenshots(DVJS10):