> For the complete documentation index, see [llms.txt](https://docs.craft.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.craft.co/alerts-api/alerts-api-queries/filtering-and-sorting.md).

# Filtering and sorting

### Filtering

Alerts API provides capabilities to filter and sort its output. It's achieved by using the `where` argument on the `alerts` node of the Craft API.

`where` allows constructing an expression that will be matched against the available Alerts and filter. The general syntax is to use the field from the Alert entity and compare it with a certain value using a specific operator.

```graphql
alerts(
  where: {
    field: { operator: value }
  }
)
```

Please, see the [AlertBoolExpression](/alerts-api/alerts-api-reference/filter-object.md#alertboolexpression) type definition for all of the available fields and operators available.

It's also possible to specify multiple fields which will be combined with a logical "AND":

```graphql
alerts(
  where: {
    firstField: { operator: value },
    secondField: { operator: value }
  }
)
```

In such case, API will return Alerts where `firstField` and `secondField` are matching provided values.

#### Filter examples

```graphql
query GetAlerts($where: AlertsBoolExpression!) {
  alerts(where: $where) {
    id
    company {
      id
    }
    text
    title
  }
}
```

For the query above, `$where` variable may have following shapes:

```json5
[
  {
    // Will filter by either "company.id" 
    // or "relatedCompanies.id"
    company: { id: { eq: 429 } }
  }, 
  {
    // String | Int can be used interchangeably for
    // the ID type
    company: { id: { eq: "429" } }
  }, 
  {
    // Find alerts for any of the listed companies
    company: { id: { in: ["429", "229"] } }
  },
  {
    // Will find Alerts where the Company id is 429 
    // and dataset is either "jobs" or "pnl"
    company: { id: { eq: 429 } },
    dataset: { in: ["jobs", "pnl"] }
  },
  {
    // Will find all "positive" Alerts with the
    // given class code
    polarity: { gt: 0 },
    class: { code: { eq: "awards" } }
  },
  {
    publishedAt: {
      // Multiple operators can be combined together
      // and will be executed with logical "AND".
      // This filter will find alerts published
      // between the given dates.
      gte: "2022-02-01",
      lt: "2022-03-01"
    }
  }
]
```

### Sorting

By default Alerts API output is sorted chronologically by `publishedAt` field in descending (most recent Alerts first) order. To change the order to ascending order (oldest published Alerts first), it's possible to specify the `orderBy` argument on the `alerts` node:

```graphql
query GetAlerts {
  alerts(
    orderBy: { publishedAt: ASC }
  )
}
```

Please, see the [orderBy](/alerts-api/alerts-api-reference/order-object.md#alertsortorder) type definition for all the possible fields and values.
