title: jQuery Sort both ascending and descending (Not a table)
body: <p>I'm trying to sort a list of results by price and rating. For simplicity, I've just included 2 results: Rider's Lodge and Olina Lodge. I have written a function that sorts it one way.</p>
  
  <p>How can I re-use the same function to sort my results such that clicking on the 'Sort by Price' sorts by ascending, clicking on it again sorts by descending and it toggles between the two? </p>
  
  <pre><code>&lt;div class="tab-content"&gt;
      &lt;div id="filters"&gt;
          &lt;p&gt;
              &lt;a href="javascript:sort_by_rating();"&gt;Sort by Rating&lt;/a&gt;
              &lt;br&gt;
              &lt;a href="javascript:sort_by_price();"&gt;Sort by Price&lt;/a&gt;
          &lt;/p&gt;
      &lt;/div&gt;
  
  
      &lt;div id="div_hotel_results" class="tab-pane active"&gt;
          &lt;form id="hotel-form"&gt;
          ...
          &lt;/form&gt;
          &lt;div class="results-row"&gt;
              &lt;h5&gt;Riders Lodge&lt;/h5&gt;
              &lt;span class="label label-info price"&gt;$103.64&lt;/span&gt;
              &lt;span class="rating" data-rating="3.0"&gt;3.0&lt;/span&gt;
          &lt;/div&gt;
  
          &lt;div class="results-row"&gt;
              &lt;h5&gt;Olina Lodge&lt;/h5&gt;
              &lt;span class="label label-info price"&gt;$99.64&lt;/span&gt;
              &lt;span class="rating" data-rating="2.5"&gt;2.5&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  function sort_by_price () {
      var sorted_list = [];
      $('.price').each(function (index) {
          sorted_list[index] = $(this).parent().parent().remove();
          sorted_list[index]['cleaned_price'] = parseFloat($(this).text().replace(/[^0-9\.]+/g,""));
      });
  
      sorted_list.sort(function (a, b) {
          if (a['cleaned_price'] == b['cleaned_price']) {
              return 0;
          } else if (a['cleaned_price'] &gt; b['cleaned_price']) {
              return 1;
          } else {
              return -1;
          }
      });
  
      $('#hotel-form').after(sorted_list);
  }
  &lt;/script&gt;
  </code></pre>