riatsila
riatsila4mo ago

Sorting different on backend vs frontend in React

I have a weird bug where I have a query that sorts a table of tasks based on "start_date".
const tasks = await ctx.db.query("tasks")
.withIndex("by_project", (q) => q.eq("project_id", projectId))
.order("asc")
.collect();
console.log(tasks);
return tasks;
const tasks = await ctx.db.query("tasks")
.withIndex("by_project", (q) => q.eq("project_id", projectId))
.order("asc")
.collect();
console.log(tasks);
return tasks;
When logging on the backend the sort is completed correctly, but the object received on the frontend is sorted by _creationtime. const tasks = useQuery(api.tasks.getByProjectId, project?._id ? {projectId: project._id}: "skip"); Can sort tasks again on the frontend, but wondering if there's anything obvious I'm missing that would cause this behaviour!
4 Replies
Convex Bot
Convex Bot4mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Ask in the <#1228095053885476985> channel to get a response from <@1072591948499664996>. - Avoid tagging staff unless specifically instructed. Thank you!
lee
lee4mo ago
how is the "by_project" index defined? i can't think of anything that would cause the sort order to change between backend and frontend; they're just opaque values so they won't be reordered
riatsila
riatsilaOP4mo ago
.index("by_project", ["project_id", "start_date", "end_date"]) — I also tried removing "end_date" to see if anything changed Thanks for your help, realised it was a rogue sort in place affecting the object 🫠
lee
lee4mo ago
ah that would do it, no worries

Did you find this page helpful?