Need help with sorting feature (dataviewjs)

Hello ! I’ve created a block of code with dataviewjs to track the projects that i’m working on, depending on a specific date. I would like it to be sorted by priority (which is a frontmatter in my notes), but it doesn’t work and I don’t figure out why. Here is the code (there is some French text inside, nothing important). I don’t have a specific error message, it just doesn’t sort.

let pages = dv.pages("#main")
let days = [] //contient tous les jours où on vérifie
let list = [] //contient toutes les cartes à afficher
let current = "<% tp.file.title %>"
let date = new Date(current)
days.push(current)
get_past_week(date)
function get_past_week (date) {
	for (let i=0; i<3; i++) {
	let date2 = date
	date2.setDate(date.getDate() - 1)
	let yyyy = date2.getFullYear();
	let mm = date2.getMonth() + 1; // Months start at 0!
	mm = addZ(mm)
	function addZ(n) {
		return n<10? '0'+n:''+n;
	}
	let dd = date2.getDate();
	days.push(`${yyyy}-${mm}-${dd}`)
	}
}
//On stock toutes les cartes à afficher
for (let page of pages) {
	let folder = page.file.folder
	folder = '"' + folder + '"'
	let allpage = dv.pages(folder) //On va vérifier également toutes les cartes relatives au projet (contenu dans le dossier portant le nom de la carte main)
	for (let day of days) {
		if (String(allpage.modified).includes(`${day}`)) {
			list.push(page)
		}
		if (String(allpage.created).includes(`${day}`)) {
			list.push(page)
		}
	}
}
//On nettoie le tableau des pages similaires 
list = uniq(list)
function uniq(a) {
    var prims = {"boolean":{}, "number":{}, "string":{}}, objs = [];

    return a.filter(function(item) {
        var type = typeof item;
        if(type in prims)
            return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);
        else
            return objs.indexOf(item) >= 0 ? false : objs.push(item);
    });
}
//On affiche le tableau que si il y a des pages à afficher
if (list.length !== 0) {
	dv.header(5, "🔬 En cours")
	dv.table(["Note", "Priority"],
		list
        .sort(p => p.priority)
        .map(p => [
			 p.file.link,
			 p.priority,
		])
	)
	dv.paragraph("")
}

I’ve tried to move the “sort” part at different locations, do you have any idea on what modifications I should do ?
Thank you !

You’re sorting list, which if I’m not mistaken doesn’t contain objects with a priority property, so basically every object in the list is equal to the others as all don’t have any priority…

You need to either need to dereference into the objects of the lists to access the priority, or change what you’re using within the list to allow for sorting on the priority property.

I hope this makes sense, and that I’ve read your code correctly, as it’s slightly confusing to read and debug without knowing the data structure which you run this query against.

Thank you for your answer.

Well, in every file there is a priority property. When I list all my selected pages with dv.paragraph(list), there is also the priority property in the frontmatter. I don’t understand why I can’t access that when sorting the table…

I’ve tried to replace

if (list.length !== 0) {
dv.header(5, “:microscope: En cours”)
dv.table([“Note”, “Priority”],
list
.sort(p => p.priority)
.map(p => [
p.file.link,
p.priority,
])
)
dv.paragraph(“”)
}

with

if (list.length !== 0) {
dv.header(5, “:microscope: En cours”)
dv.table([“Note”, “Priority”],
dv.pages(“#main”)
.sort(p => p.priority)
.map(p => [
p.file.link,
p.priority,
])
)
dv.paragraph(“”)
}

and the sorting is working, but it obviously doesn’t show the cards that i’ve selected in the first part of the code, but all cards with tag #main

Alright, I solved the issue with a little trick. I just wright the query with the paths of my selected files. Here is the last part code :

//On nettoie le tableau des pages similaires 
list = uniq(list)
function uniq(a) {
    var prims = {"boolean":{}, "number":{}, "string":{}}, objs = [];

    return a.filter(function(item) {
        var type = typeof item;
        if(type in prims)
            return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);
        else
            return objs.indexOf(item) >= 0 ? false : objs.push(item);
    });
}
let path = []
for (let i = 0; i<list.length; i++) {
	path.push(list[i].file.path)
}
let query = '"' + path[0] + '"'
for (let i = 1; i<list.length; i++) {
	query = query + " or " + '"' + path[i] + '"'
}
//On affiche le tableau que si il y a des pages à afficher
if (list.length !== 0) {
	dv.header(5, "🔬 En cours")
	dv.table(["Note", "Priority"],
	dv.pages(`${query}`)
  .sort(p => p.priority, "desc")
		.map(p => [
			 p.file.link,
			 p.priority,
		])
	)
	dv.paragraph("")
}

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