Sorting dataview JS table by progress

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

I would like this following dataviewjs table query to sort desc by the highest progress made.

I want to be able to see which series I have the most progress with. Here is the query.

let pages = dv.pages("#s/HR💟").where(p => p.seriesFinished == null && p.seriesStarted !== null) // Source

dv.table( //Create the table 
	["Title", "Books Read", "Progress", "Up Next/Current"],
	//Define the columns
	pages.map(p => { //Map `pages` to the columns 
			const booksInSeries = p.file.inlinks; 
			let booksReadLinks = [];
			booksInSeries.forEach(b => { //Loop through all the books in a series 
				const book = dv.page(b.path) //Process the link (since inlinks were retrieved) into a file 
				if (book.file.tags.includes("#read✅")) { //Check if the file's tags includes... 
				booksReadLinks.push(book.file.link) //Push the book's file link into `booksReadLinks` 
			} 
		}) 
		let upnext = [];
			booksInSeries.forEach(b => { //Loop through all the books in a series 
				const book = dv.page(b.path) //Process the link (since inlinks were retrieved) into a file 
				if (book.file.tags.includes("#upnext")) { //Check if the file's tags includes... 
				upnext.push(book.file.link) //Push the book's file link into `upnext`
				
			} 
		})			
		const booksReadOverTotal = `${booksReadLinks.length}/${booksInSeries.length}`
		 /* Progress Bar */ 
		 let progress; 
		 const value = Math.round((booksReadLinks.length / booksInSeries.length || 0) * 100); //Divide the books read by books in series, multiply by 100 and round 
		 if (value == 100) { //If the value is 100/100 (complete), give a special class to the progress bar (for stylizing); optional 
			 progress = `${value}% <progress class='complete' value='100' max='100'></progress>`; 
			 } else { 
				 progress = `${value}% <progress value='${value}' max='100'></progress>`; 
		 } 
		 return [p.file.link, booksReadOverTotal, progress, upnext] 
		 }))

Things I have tried

I have looked on the forum and discord to see if there are any other example that I could used to figure it out on my own, but I have not been successful. I am not sure where I need to add the sort command. Can anyone help?

If you’re lucky you could add the following at the end of the script (before the last parenthesis):

  .sort(p => p[3] )

In other words that the script should resemble something like:

```dataviewjs
let pages = dv.pages("#s/HR💟")
  .where(p => p.seriesFinished == null && p.seriesStarted !== null) // Source

dv.table( //Create the table 
  ["Title", "Books Read", "Progress", "Up Next/Current"],
  //Define the columns
  pages
    .map(p => {
      ...
      return [p.file.link, booksReadOverTotal, progress, upnext] 
    })
    .sort(p => p[2])
)
```

I would also suggest not using the tab characters, and rather use an indentation of two spaces, along with shifting to the next line when you do daisy chaining like .map() and .sort(), as I find it easier to read the structure and what belongs to which block.

Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks or queries) is properly shown.

It didn’t work. I’m not sure why. Also, thank you for the bonus tip.

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