<p>I have an Element. It's a Structure, with one level (not sure if that is relevant). I'd like to add a Sort Option that allows a user to sort the Element Index page for this Element by a number. The number attribute is stored in the database as an <code>int</code> and named <code>count</code>.</p>

<p>On the Element class, I can add a way to sort the Element by a row alphabetically:</p>

<pre><code>protected static function defineSortOptions(): array
{
  $attributes = [
    'someTextField' =&gt; Craft::t('my-plugin', 'Some Text Field')
  ]
}
</code></pre>

<p>However, I haven't yet been able to figure out how to give the option to sort by a number:</p>

<pre><code>protected static function defineSortOptions(): array
{
  $attributes = [
    'someTextField' =&gt; Craft::t('my-plugin', 'Some Text Field'),
    'count' =&gt; Craft::t('my-plugin', 'Count')
  ]
}
</code></pre>

<p>The above example does not work. I've also explored the non-shorthand syntax that I see in the Craft Commerce Subscription sort options, however, have been unable to figure out how to adjust things to work with numbers.</p>

<pre><code>protected static function defineSortOptions(): array
{
    return [
        [
            'label' =&gt; Craft::t('commerce', 'Subscription date'),
            'orderBy' =&gt; 'commerce_subscriptions.dateCreated',
            'attribute' =&gt; 'dateCreated'
        ],
    ];
}
</code></pre>

<p>How can I allow a user to order an Element Index page by a numeric column?</p>

CC BY-SA 4.0
2019-02-23T18:24:53.023Z
Did you define `count` as tableattribute as well?
Robin Schambach
CC BY-SA 4.0
2019-02-24T22:26:46.773Z
Yep. ```public static function defineTableAttributes($source = null): array
    {
        return [
            'count' => Craft::t('my-plugin', 'Count')
        ];
    }```
Ben Parizek
CC BY-SA 4.0
2019-02-24T23:01:15.320Z
I'm not totally sure if that's really the issue but maybe it could have something to do with the word `count` in yii2 and queries. I'll investigate more time in a few hours but maybe you could try to rename it to `count2` or something else and see if it solves the issue. In general there is no difference between text and number columns in the DB. Just a question for now: what didn't work? Was the column not sortable or did it throw an exception?
Robin Schambach
CC BY-SA 4.0
2019-02-25T00:12:30.537Z
No errors, It's just that the column does not sort anything. It always displays the same order.
Ben Parizek
<elementtype><elementquery>
How to add an Element sortOption to sort by number?
# How to add an Element sortOption to sort by number?

(Asked by: username_0 on 2019-02-23)

I have an Element. It's a Structure, with one level (not sure if that is relevant). I'd like to add a Sort Option that allows a user to sort the Element Index page for this Element by a number. The number attribute is stored in the database as an int and named count.
On the Element class, I can add a way to sort the Element by a row alphabetically:
protected static function defineSortOptions(): array
{
  $attributes = [
    'someTextField' => Craft::t('my-plugin', 'Some Text Field')
  ]
}

However, I haven't yet been able to figure out how to give the option to sort by a number:
protected static function defineSortOptions(): array
{
  $attributes = [
    'someTextField' => Craft::t('my-plugin', 'Some Text Field'),
    'count' => Craft::t('my-plugin', 'Count')
  ]
}

The above example does not work. I've also explored the non-shorthand syntax that I see in the Craft Commerce Subscription sort options, however, have been unable to figure out how to adjust things to work with numbers.
protected static function defineSortOptions(): array
{
    return [
        [
            'label' => Craft::t('commerce', 'Subscription date'),
            'orderBy' => 'commerce_subscriptions.dateCreated',
            'attribute' => 'dateCreated'
        ],
    ];
}

How can I allow a user to order an Element Index page by a numeric column?

**Tags:** elementtype, elementquery

> **Comments:**
> **username_1**: Did you define `count` as tableattribute as well?  
> **username_0**: Yep. ```public static function defineTableAttributes($source = null): array     {         return [             'count' => Craft::t('my-plugin', 'Count')         ];     }```  
> **username_1**: I'm not totally sure if that's really the issue but maybe it could have something to do with the word `count` in yii2 and queries. I'll investigate more time in a few hours but maybe you could try to rename it to `count2` or something else and see if it solves the issue. In general there is no difference between text and number columns in the DB. Just a question for now: what didn't work? Was the column not sortable or did it throw an exception?  
> **username_0**: No errors, It's just that the column does not sort anything. It always displays the same order.  

---

## 0 answers

