maros.studenicM
Convex Community2y ago
14 replies
maros.studenic

Hey I wanted to ask, is there a way to

Hey I wanted to ask, is there a way to specify by which field you want to sort collection with index ?
for example
I have list of companies that have year revenue, and want to filter them based on the revenue, but I want to sort them based on creation date.


for example
export const getCompanies = query({
    args: {
        minRevenue: v.number(),
        maxRevenue: v.number()
    },
    handler: async (ctx, args) => {
        const companies = await ctx.db.query("companies").withIndex('revenue', (q) => q.and(q.lte(q.field("revenue"), args.maxRevenue), q.gte(q.field("revenue"), args.minRevenue)),order('desc');
        return companies;
    }
});


Because now I understand that is going to be sorted by revenue, because its first field in index.
Was this page helpful?