title: sort ruby hash when using json
body: <p>I have a Ruby hash which I'm converting <code>to_json</code> and passing to a view. JavaScript then parses the JSON (<code>JSON.parse</code>) and iterates it, spewing out the key, value pairs. How do I sort the hash so that my key/value pairs are ordered alphabetically by key? This can be done before passing to the view or in JavaScript as long as the final result is an ordered list of key/values. <code>hash.sort</code> does not work, neither does <code>hash.to_a.sort</code>... If I do this, I find that I cannot iterate properly and get both key and value.</p>
  
  <p><code>for (key in myHash) {alert(key); alert(myHash[key]); }</code> doesn't work once sort is applied.</p>