title: casting string to json and ordering by value as int
body: <p>I have a string field in my database that contains a JSON value. I can sort the rows by this value by casting to <code>json</code> and accessing the value, like so:</p>
  
  <pre><code>SELECT id FROM table ORDER BY stringvalue::json-&gt;&gt;'key' ASC
  </code></pre>
  
  <p>However, this will sort alphabetically, but the 'key' contains a numeric value, so instead of 1-2-...-10-11 it will go 1-10-11-2-...</p>
  
  <p>The documentation suggests that <code>(jsonvalue-&gt;&gt;'m148')::int ASC</code> should work, but I get "aggregate function needs to appear in grouping column" when trying this, I guess due to the fact that I don't have a <code>json</code> field, but a string field that I'm already casting to <code>json</code>;</p>
  
  <pre><code>SELECT id FROM table ORDER BY (stringvalue::json-&gt;&gt;'key')::int ASC
  </code></pre>
  
  <p>Any suggestions on how I would keep this format of casting to <code>json</code> while also sorting numerically?</p>