title: sort array based on conditions using javascript sort function
body: <p>I want to sort an javascript array based on numbers but I wanted a specific group (e.g. Recommended) comes first and ascending, followed by other group that has no types.</p>
  
  <p>Below are the divs, I wanted to sort in such a way that the Recommended comes first and in ascending order followed by other divs in ascending order.</p>
  
  <p><a href="https://i.stack.imgur.com/96Z9s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/96Z9s.png" alt="enter image description here"></a></p>
  
  <p>Below code is sorting the divs with no <strong>data-type</strong> in ascending order correctly but the <strong>Recommended</strong> is not sorting in ascending order as expected.</p>
  
  <pre><code>var sortedDivs = $(".terminalpopular").toArray().sort(sorter);
  console.log(sortedDivs); 
  
  function sorter(a, b) {
      if(a.getAttribute('data-type') == 'Recommended'){
          if (parseFloat(a.getAttribute('data-amount')) &lt; parseFloat(b.getAttribute('data-amount'))){
            return -1;
          }
          return 0;
      }
      else{
        if (parseFloat(a.getAttribute('data-amount')) &gt; parseFloat(b.getAttribute('data-amount'))){
          return 1;
        }
        return 0;
      }
    }
  </code></pre>
  
  <p>Above code sorts the divs in such form as;</p>
  
  <pre><code>&lt;div class="hotel-list listing-style3 hotel" id="list_view_listingpopular"&gt;
      &lt;div class="terminalpopular" data-amount="65.95" data-type="Recommended"&gt;
      &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="94.95" data-type="Recommended"&gt;  &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="70" data-type="Recommended"&gt;
      &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="54.95" data-type=""&gt;
      &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="67.99" data-type=""&gt;
      &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="75" data-type=""&gt;
      &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="78" data-type=""&gt;
      &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="84.99" data-type=""&gt;
      &lt;/div&gt;
      &lt;div class="terminalpopular" data-amount="84.99" data-type=""&gt;
      &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>As, it can be seen that divs without any <strong>data-type</strong> is sorted correctly but <strong>Recommended</strong> divs are not sorted in ascending order as expected.</p>
  
  <p>Note: It does come in IF clause and condition does meet i.e. <code>(if(a.getAttribute('data-type') == 'Recommended'){})</code>. I have put console in IF and it does compare values.</p>
  
  <p>Please, help. Thanks</p>