1

I'm displaying a list of tags in Gatsby using this query:

  query={graphql`
    query {
      tags: allMarkdownRemark {
        group(field: frontmatter___tags) {
          fieldValue
          totalCount
        }
      }
    }
  `}

At the moment it includes all the tags ever used. I want to limit the list to the top 10 most popular tags. In order to do this I need to sort them first by totalCount. How can I accomplish this?

Lizzy
  • 13
  • 3

1 Answers1

1

I do not think that you can sort them and then limit them using just a graphql query. My process for this would be to query the entire list of tags just like you have it, and then use javascript after that to manipulate the list of objects. There is a post that covers how to sort a list just like the one you will get back from this here:

Sorting Objects by Property Values

This would then return an array, and you could loop over the array for the first ten objects.

NickC
  • 332
  • 1
  • 15