Skip to main content
The AnimeThemes GraphQL API allows you to sort query results using the sort argument on pagination queries.

Basic Sorting

Sort results using the sort argument with a field and direction.

Sort by Year (Ascending)

query {
  animePagination(
    first: 10,
    sort: [{ field: YEAR, order: ASC }]
  ) {
    data {
      name
      year
    }
  }
}

Sort by Year (Descending)

query {
  animePagination(
    first: 10,
    sort: [{ field: YEAR, order: DESC }]
  ) {
    data {
      name
      year
    }
  }
}

Sort Directions

Two sort directions are available:
DirectionDescription
ASCAscending order (A-Z, 0-9, oldest to newest)
DESCDescending order (Z-A, 9-0, newest to oldest)

Multiple Sort Fields

You can sort by multiple fields. Results are sorted by the first field, then by the second field for rows with matching values, and so on.
query {
  animePagination(
    first: 20,
    sort: [
      { field: YEAR, order: DESC },
      { field: SEASON, order: ASC },
      { field: NAME, order: ASC }
    ]
  ) {
    data {
      name
      year
      season
    }
  }
}
This query sorts anime by year (newest first), then by season within each year, then alphabetically by name.

Sortable Fields

Each resource type has specific sortable fields. Use the GraphiQL sandbox to explore available fields.

Common Sortable Fields

Anime:
  • ID
  • NAME
  • SLUG
  • YEAR
  • SEASON
  • MEDIA_FORMAT
  • CREATED_AT
  • UPDATED_AT
Artist:
  • ID
  • NAME
  • SLUG
  • CREATED_AT
  • UPDATED_AT
Video:
  • ID
  • BASENAME
  • FILENAME
  • RESOLUTION
  • NC
  • SUBBED
  • LYRICS
  • UNCEN
  • SOURCE
  • OVERLAP
  • CREATED_AT
  • UPDATED_AT
Theme:
  • ID
  • TYPE
  • SEQUENCE
  • GROUP
  • SLUG
  • CREATED_AT
  • UPDATED_AT

Combining Sort with Filters

Sorting works seamlessly with filtering:
query {
  animePagination(
    first: 10,
    where: [
      { field: YEAR, operator: GTE, value: 2020 }
    ],
    sort: [{ field: YEAR, order: DESC }]
  ) {
    data {
      name
      year
      season
    }
  }
}

Sorting Relations

You can also sort nested relations:
query {
  anime(slug: "bakemonogatari") {
    name
    themes(sort: [{ field: SEQUENCE, order: ASC }]) {
      id
      type
      sequence
      slug
    }
  }
}

Random Sorting

Some resources support random sorting for shuffled results. This is useful for features like “random theme” or “shuffle playlist”.
query {
  animePagination(
    first: 5,
    sort: [{ field: RANDOM }]
  ) {
    data {
      name
      year
    }
  }
}
Random sorting may have performance implications on large datasets. Use it with appropriate pagination limits.

Pivot Table Sorting

When querying many-to-many relationships, you can sort by fields on the pivot table:
query {
  anime(slug: "bakemonogatari") {
    name
    studios(sort: [{ field: PIVOT_CREATED_AT, order: DESC }]) {
      name
    }
  }
}

Practical Examples

Latest Anime First

query {
  animePagination(
    first: 20,
    sort: [
      { field: YEAR, order: DESC },
      { field: CREATED_AT, order: DESC }
    ]
  ) {
    data {
      name
      year
      season
    }
  }
}

Alphabetical Listing

query {
  animePagination(
    first: 50,
    sort: [{ field: NAME, order: ASC }]
  ) {
    data {
      name
      slug
    }
  }
}

Themes by Type and Sequence

query {
  anime(slug: "bakemonogatari") {
    name
    themes(sort: [
      { field: TYPE, order: ASC },
      { field: SEQUENCE, order: ASC }
    ]) {
      type
      sequence
      slug
      song {
        title
      }
    }
  }
}
This sorts themes with OPs first, then EDs, each in sequence order.

Recent Videos by Resolution

query {
  videoPagination(
    first: 20,
    sort: [
      { field: CREATED_AT, order: DESC },
      { field: RESOLUTION, order: DESC }
    ]
  ) {
    data {
      basename
      filename
      resolution
      created_at
    }
  }
}

Sort with Pagination

Sorting is particularly useful with pagination to ensure consistent ordering across pages:
query {
  animePagination(
    first: 25,
    page: 2,
    sort: [{ field: NAME, order: ASC }]
  ) {
    data {
      name
      slug
    }
    paginationInfo {
      currentPage
      lastPage
      total
    }
  }
}
Without explicit sorting, the order of results may be inconsistent across pagination pages. Always use sort when paginating.

Best Practices

Specify a sort order when using pagination to ensure consistent results across pages.
For better performance, use indexed fields like ID, CREATED_AT, or YEAR as the primary sort field.
Include a unique field like ID as the last sort criterion to ensure stable, deterministic ordering.
Sorting large datasets can be expensive. Combine with filters to reduce the dataset before sorting.

Next Steps

Pagination

Learn how to paginate sorted results

Filtering

Filter data before sorting

Build docs developers (and LLMs) love