Answers.Body: <p>If you encounter problems with assigning variables as the keys, what about putting all your values into objects?</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set catObjects = [] %}
  
  {% for cat in allCats %}
  
      {% set catObject = [
          { catId: cat.id, catCount: craft.entries.relatedTo(cat).total()  }
      ] %}
  
      {% set catObjects = catObjects|merge(catObject) %}
  
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T20:43:50.990Z
Answers.LastActivityDate: 2014-07-30T20:43:50.990Z
Answers.UserDisplayName: carlcs
Body: <p>I have modified the code from <a href="https://craftcms.stackexchange.com/questions/1086/order-tags-by-count-e-g-top-10-tags-used">this question</a> but I'm running into an issue. </p>
  
  <pre><code>{% set allCats = craft.categories.group('blogCategories').level(1).find() %}
  
  {% set catArray = {} %}
  
  {% for cat in allCats %}
  
      {% set catArray = catArray | merge({(cat.id):craft.entries.relatedTo(cat).total() }) %}
  
  {% endfor %}
  
  {% set catArray = catArray|sort|reverse %}
  
  {% for catId, count in catArray %}
  
      {% set category = craft.categories.id(catId).first() %}
  
      &lt;li&gt;
          &lt;a href="{{ category.url }}"&gt;{{ category.title }} &lt;span&gt;{{count}}&lt;/span&gt;&lt;/a&gt;
      &lt;/li&gt;
  
  {% endfor %}
  </code></pre>
  
  <p>Ive ran a dump on the catArray array and the category id isnt being set as the key, regardless of if I try put brackets around it to protect it, I only get the value in the array.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-30T20:12:05.003Z
Comments.Text: Did you try the code that I provided (totally untested from my side) in this other Q/A? Ohh and I think we should better close this thread here and "discuss" your problems over there...
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-30T20:12:44.173Z
Comments.Text: possible duplicate of [Order tags by count - e.g. Top 10 Tags Used](http://craftcms.stackexchange.com/questions/1086/order-tags-by-count-e-g-top-10-tags-used)
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-30T20:20:24.113Z
Comments.Text: Sean, I wonder why I (and you too) put the `cat.id` in brackets? Actually no reason to do this..
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-30T20:27:37.903Z
Comments.Text: I don't think it's identical. The context is different and the issue I have hasn't been mentioned there. The reason you (and therefore I) have it in parentheses is due to [how Twig treats keys](http://stackoverflow.com/questions/14638231/twig-using-variables-as-hash-key). It does work when I use a `category.slug` as a key, but `category.id` does not want to work.
Comments.UserDisplayName: Seán O'Grady
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-30T20:30:17.103Z
Comments.Text: Yes not really completely a duplicate, but if you are running into problems those exact problems are laying in that other Q/A, too.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-30T20:33:52.197Z
Comments.Text: I think the issue isnÄt mentioned there because nobody actually tested the code :( ...I wonder who made those upvotes then, hehe?! Ohh and thanks for reminding me where those brackets come from.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-30T20:44:51.557Z
Comments.Text: Strange that it doesn't work, Sean. But I have an idea how it should definitely work! See answer below!
Comments.UserDisplayName: carlcs
Tags: <templating><array>
Title: Order a list of categories by number of entries, key not being set
ThreadText: # Order a list of categories by number of entries, key not being set
  
  (Asked by: username_0 on 2014-07-30)
  
  I have modified the code from [this question](https://craftcms.stackexchange.com/questions/1086/order-tags-by-count-e-g-top-10-tags-used) but I'm running into an issue.
  
  ```
  {% set allCats = craft.categories.group('blogCategories').level(1).find() %}
  
  {% set catArray = {} %}
  
  {% for cat in allCats %}
  
      {% set catArray = catArray | merge({(cat.id):craft.entries.relatedTo(cat).total() }) %}
  
  {% endfor %}
  
  {% set catArray = catArray|sort|reverse %}
  
  {% for catId, count in catArray %}
  
      {% set category = craft.categories.id(catId).first() %}
  
      <li>
          <a href="{{ category.url }}">{{ category.title }} <span>{{count}}</span></a>
      </li>
  
  {% endfor %}
  
  ```
  
  Ive ran a dump on the catArray array and the category id isnt being set as the key, regardless of if I try put brackets around it to protect it, I only get the value in the array.
  
  **Tags:** templating, array
  
  > **Comments:**
  > **username_1**: Did you try the code that I provided (totally untested from my side) in this other Q/A? Ohh and I think we should better close this thread here and "discuss" your problems over there...  
  > **username_1**: possible duplicate of [Order tags by count - e.g. Top 10 Tags Used](http://craftcms.stackexchange.com/questions/1086/order-tags-by-count-e-g-top-10-tags-used)  
  > **username_1**: Sean, I wonder why I (and you too) put the `cat.id` in brackets? Actually no reason to do this..  
  > **username_0**: I don't think it's identical. The context is different and the issue I have hasn't been mentioned there. The reason you (and therefore I) have it in parentheses is due to [how Twig treats keys](http://stackoverflow.com/questions/14638231/twig-using-variables-as-hash-key). It does work when I use a `category.slug` as a key, but `category.id` does not want to work.  
  > **username_1**: Yes not really completely a duplicate, but if you are running into problems those exact problems are laying in that other Q/A, too.  
  > **username_1**: I think the issue isnÄt mentioned there because nobody actually tested the code :( ...I wonder who made those upvotes then, hehe?! Ohh and thanks for reminding me where those brackets come from.  
  > **username_1**: Strange that it doesn't work, Sean. But I have an idea how it should definitely work! See answer below!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-07-30. Score: 2 | Accepted answer
  
  If you encounter problems with assigning variables as the keys, what about putting all your values into objects?
  
  ```twig
  {% set catObjects = [] %}
  
  {% for cat in allCats %}
  
      {% set catObject = [
          { catId: cat.id, catCount: craft.entries.relatedTo(cat).total()  }
      ] %}
  
      {% set catObjects = catObjects|merge(catObject) %}
  
  {% endfor %}
  
  ```
  
  
  ---
  
  