Explanation of jQuery table sorting functionality
<p>I'd like some help with the following jQuery code if possible...</p>

<pre><code> $(document).ready(function() {
  $('table.sortable').each(function() {
   var $table = $(this);
    $('th', $table).each(function(column) { // #A
     var $header = $(this); 
      $header.click(function() {
       var rows = $table.find('tbody &gt; tr').get();
        rows.sort(function(a, b) { // #B
         var keyA = $(a).children('td').eq(column).text().toUpperCase(); //#C
         var keyB = $(b).children('td').eq(column).text().toUpperCase();
          if(keyA &lt; keyB) return -1;
          if(keyA &gt; keyB) return 1;
          return 0;
          });
          $.each(rows, function(index, row) { //#D
           $table.children('tbody').append(row);
          });
        });
     });
   });
 });
</code></pre>

<p>For the comments I've added (#A through to #D), can somebody explain to me:</p>

<p>#A - What signifies the closure/end of this function... i.e. where does the function end? I'm naively looking for a closing curly brace, otherwise from reading the code I'm assuming the whole body of code is run for each 'th' element encountered, which doesn't seem very efficient? Does one of the "});"'s at the end signify the end of this function?</p>

<p>#B - I would <strong>love</strong> to understand how this comparator function works. My book covers it really briefly and I can't find any other resources on the web! What values will be passed into the function via the arguments 'a' and 'b'? Can somebody painstakingly explain this whole function, please?! :)</p>

<p>#C - How does the .eq(column) part work? I would assume that if 'column' contains the iteration the loop is currently on, the value will always be the final iteration value? Or does this comparator function run for each iteration through the .each loop?</p>

<p>#D - This function presumably goes through each row of the sorted array <em>rows</em> and appends it to the table body. Are these two arguments (index, row) always included in the $.each callback function?</p>

<p>Any help gratefully received!! I already have an answer for #B, if you could just help by answering one of the queries, I will show my appreciation with upvotes! Thanks in advance.</p>
