title: How to sort javascript object array by element.name
body: <p>I am try to write some validation script using javascript and prototype.</p>
  
  <p>What I want to do is to loop through all the elements of a form and validate each answer.  My code works, BUT the array of DOM elements is unsorted.  I would like to sort the elements by their ID.</p>
  
  <p>Here is the my code, which works fine if I comment-out elem.sort(zelementsort);</p>
  
  <pre><code>function zelementsort(a,b) {
      if (a.name &gt; b.name)
          return -1;
      else if (b.name &gt; a.name)
          return 1;
      else 
          return 0;
  }   
  
  var elem = document.getElementById('myform').elements;
  elem.sort(zelementsort);
  
  for(var i = 0; i &lt; elem.length; i++)
  {
       alert("Name = " + elem[i].name);
  
  }
  </code></pre>
  
  <p>I wonder if the problem might be that some of the elements do not have names.  Anyone have another simpler way of sorting an array of DOM elements by their .name?</p>