Answers.Body: <p>You can try this.</p>
  <pre><code>query getElements {
    entries(
      section: [&quot;anEntryType&quot;]
      orderBy: [
        {
          field: &quot;numberField&quot;
          direction: &quot;ASC&quot;
        },
        {
          field: &quot;numberField&quot;
          direction: &quot;ASC&quot;
          value: null
        }
      ]
    ) {
      id
      ... on anEntryType_default_Entry {
        numberField
      }
    }
  }
  
  </code></pre>
  <p>or</p>
  <p>To sort the entries with a valid numberField value first, followed by the entries with a null value for numberField, you can use the following orderBy parameter in your query:</p>
  <pre><code>orderBy: [{
    field: &quot;numberField&quot;,
    direction: &quot;ASC&quot;,
    custom: &quot;IS NOT NULL&quot;
  }, {
    field: &quot;numberField&quot;,
    direction: &quot;ASC&quot;
  }]
  </code></pre>
  <p>The custom parameter allows you to specify a custom sorting function that is applied after the initial sorting based on the field and direction parameters. The value &quot;IS NOT NULL&quot; tells Craft CMS to sort the entries with a non-null value for numberField before the entries with a null value for numberField.</p>
  <p>Here is an example of how your GraphQL query would look with this sorting function included:</p>
  <pre><code>query getElements {
    entries(
      section: [&quot;anEntryType&quot;],
      orderBy: [{
        field: &quot;numberField&quot;,
        direction: &quot;ASC&quot;,
        custom: &quot;IS NOT NULL&quot;
      }, {
        field: &quot;numberField&quot;,
        direction: &quot;ASC&quot;
      }]
    ) {
      id
      ... on anEntryType_default_Entry {
        numberField
      }
    }
  }
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-20T21:39:38.387Z
Answers.LastActivityDate: 2022-12-20T21:39:38.387Z
Answers.UserDisplayName: JBrew30
Body: <p>I am attempting to query entries via that have a field (ex <code>numberField</code>)  with field type Number. Some of the entries don't have a value for this field, so in the response it has <code>null</code>. I am wanting to order the entries by this Number field, but I am wanting the ones with a valid number to be ordered first, and then the ones with values of <code>null</code> to come after. Examples below:</p>
  <p><strong>Rough query</strong></p>
  <pre><code>query getElements {
    entries(
      section: [&quot;anEntryType&quot;]
      orderBy: &quot;numberField ASC&quot;
    ) {
      id
      ... on anEntryType_default_Entry {
        numberField
      }
    }
  }
  </code></pre>
  <p><strong>Response</strong></p>
  <pre><code>{
    &quot;data&quot;: {
      &quot;entries&quot;: [
        {
          &quot;id&quot;: &quot;1&quot;,
          &quot;numberField&quot;: null
        },
        {
          &quot;id&quot;: &quot;2&quot;,
          &quot;numberField&quot;: null
        },
        {
          &quot;id&quot;: &quot;3&quot;,
          &quot;numberField&quot;: 100
        },
        {
          &quot;id&quot;: &quot;4&quot;,
          &quot;numberField&quot;: 200
        },
      ]
    }
  }
  </code></pre>
  <p><strong>Desired Response</strong></p>
  <pre><code>{
    &quot;data&quot;: {
      &quot;entries&quot;: [
        {
          &quot;id&quot;: &quot;3&quot;,
          &quot;numberField&quot;: 100
        },
        {
          &quot;id&quot;: &quot;4&quot;,
          &quot;numberField&quot;: 200
        },
        {
          &quot;id&quot;: &quot;1&quot;,
          &quot;numberField&quot;: null
        },
        {
          &quot;id&quot;: &quot;2&quot;,
          &quot;numberField&quot;: null
        },
      ]
    }
  }
  </code></pre>
  <p>Is this possible?
  Thanks!</p>
  
Tags: <craft3><entries><ordering><graphql>
Title: GraphQL query orderBy number including null
ThreadText: # GraphQL query orderBy number including null
  
  (Asked by: username_0 on 2022-07-11)
  
  I am attempting to query entries via that have a field (ex `numberField`) with field type Number. Some of the entries don't have a value for this field, so in the response it has `null`. I am wanting to order the entries by this Number field, but I am wanting the ones with a valid number to be ordered first, and then the ones with values of `null` to come after. Examples below:
  
  **Rough query**
  
  ```
  query getElements {
    entries(
      section: ["anEntryType"]
      orderBy: "numberField ASC"
    ) {
      id
      ... on anEntryType_default_Entry {
        numberField
      }
    }
  }
  
  ```
  
  **Response**
  
  ```
  {
    "data": {
      "entries": [
        {
          "id": "1",
          "numberField": null
        },
        {
          "id": "2",
          "numberField": null
        },
        {
          "id": "3",
          "numberField": 100
        },
        {
          "id": "4",
          "numberField": 200
        },
      ]
    }
  }
  
  ```
  
  **Desired Response**
  
  ```
  {
    "data": {
      "entries": [
        {
          "id": "3",
          "numberField": 100
        },
        {
          "id": "4",
          "numberField": 200
        },
        {
          "id": "1",
          "numberField": null
        },
        {
          "id": "2",
          "numberField": null
        },
      ]
    }
  }
  
  ```
  
  Is this possible?
  Thanks!
  
  **Tags:** craft3, entries, ordering, graphql
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-12-20. Score: 1
  
  You can try this.
  
  ```
  query getElements {
    entries(
      section: ["anEntryType"]
      orderBy: [
        {
          field: "numberField"
          direction: "ASC"
        },
        {
          field: "numberField"
          direction: "ASC"
          value: null
        }
      ]
    ) {
      id
      ... on anEntryType_default_Entry {
        numberField
      }
    }
  }
  
  ```
  
  or
  
  To sort the entries with a valid numberField value first, followed by the entries with a null value for numberField, you can use the following orderBy parameter in your query:
  
  ```
  orderBy: [{
    field: "numberField",
    direction: "ASC",
    custom: "IS NOT NULL"
  }, {
    field: "numberField",
    direction: "ASC"
  }]
  
  ```
  
  The custom parameter allows you to specify a custom sorting function that is applied after the initial sorting based on the field and direction parameters. The value "IS NOT NULL" tells Craft CMS to sort the entries with a non-null value for numberField before the entries with a null value for numberField.
  
  Here is an example of how your GraphQL query would look with this sorting function included:
  
  ```
  query getElements {
    entries(
      section: ["anEntryType"],
      orderBy: [{
        field: "numberField",
        direction: "ASC",
        custom: "IS NOT NULL"
      }, {
        field: "numberField",
        direction: "ASC"
      }]
    ) {
      id
      ... on anEntryType_default_Entry {
        numberField
      }
    }
  }
  
  ```
  
  
  ---
  
  