Hosna Qasmei
Hosna Qasmei9mo ago

is it possible to query .order based on a variable?

I have this query I am returning it by order('desc') but im assuming is desc based on the _creationTime. Is it possible or how would I do it if I what to sort the date variable not the _creationTime variable? I included the schema and the function
No description
No description
4 Replies
lee
lee9mo ago
Queries are sorted by an index, and indexes implicitly end in _creationTime, so your query is currently sorted descending on _creationTime. To sort by date instead you could define the index on both fields ["planId","date"]. Or you could post-sort:
const markers = await ctx.db.query("markers").withIndex("by_planId",q=>q.eq("planId",args.planId)).collect();
markers.sort((a,b)=>a.date - b.date);
const markers = await ctx.db.query("markers").withIndex("by_planId",q=>q.eq("planId",args.planId)).collect();
markers.sort((a,b)=>a.date - b.date);
Hosna Qasmei
Hosna QasmeiOP9mo ago
gotcha makes sense thank you!
Hosna Qasmei
Hosna QasmeiOP9mo ago
No description
Hosna Qasmei
Hosna QasmeiOP9mo ago
Incase anyone needs it

Did you find this page helpful?