Order by not working on nested populate

System Information
  • Strapi Version: 4.15
  • Operating System: MacOS
  • Database: Postgres
  • Node Version: 18.13

I have a collection of Players, and I’m trying to populate the associated clubs and sort the players within each club by their fullname in ascending order. However, it seems that the sorting is not taking effect, and the players array remains unsorted

     const player = await strapi.db.query('api::player.player').findOne({
      where: {
        $and: [{ slug: { $eq: slug } }, { publishedAt: { $not: null } }]
      },
      populate: {
        club: {
          populate: {
            players: {
              sort: [{ fullname: 'asc' }]
            }
          }
        }
      }
    });

Since you are using the query engine it user the KNEX syntax, so you would need to use orderBy, you can learn more from the docs here

strapi.db.query('api::article.article').findMany({
  orderBy: {
    author: {
      name: 'asc',
    },
  },
});

For your above example to work, instead of using db.query you would use entityService instead.

strapi.entityService.findMany('api::article.article', {
  sort: {
    author: {
      name: 'asc',
    },
  },
});

You can see the docs here

Hope this helps.

@Paul_Brats Thanks for the explanation

i just replace sort with orderBy and its work for me following Knex Syntax
here is my sample code in-case anyone face same issue

   const player = await strapi.db.query('api::player.player').findOne({
      where: {
        $and: [{ slug: { $eq: slug } }, { publishedAt: { $not: null } }]
      },
      populate: {
        club: {
          populate: {
            players: {
              orderBy: [{ fullname: 'asc' }]
            }
          }
        }
      }
    });```
1 Like

You are welcome. And thank you for sharing the exaple.

1 Like