title: Javascript - sorting a collection of divs
body: <p>I am trying to learn Javascript alone so please don't suggest a library or jQuery.</p>
  
  <p>I have a list of divs and I want the user to be able to sort them by their value. For example:</p>
  
  <pre><code>&lt;button onclick="sort();"&gt;Test&lt;/button&gt;
  &lt;div class="num"&gt;2&lt;/div&gt;
  &lt;div class="num"&gt;3&lt;/div&gt;
  &lt;div class="num"&gt;8&lt;/div&gt;
  &lt;div class="num"&gt;1&lt;/div&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>function sort(){
     var elements = document.getElementsByClassName("num");
     elements.sort();
  }
  </code></pre>
  
  <p>I cannot find a straight answer to what's wrong with this. Does <code>getElementsByClassName</code> return an array of the values of each div with that name? How, when the array is then sorted, so I reflect the changes in the divs?</p>