title: jquery ajax order table based on Search results
body: <p>I have a 6 column DataTable that has a Search feature. When someone starts typing in the Search box the table only displays table items that match as expected. One of the columns has certain values that start with the letter 'Y', i wish that when someone searches all the results are ordered so that the table item that starts with Y is displayed first, followed by all other items that match the search query but do not start with a Y.</p>
  <p>Looking to use something like this ready function (but open to better ideas)</p>
  <pre><code>function setupTable() { 
      var table = $('#locTable').DataTable({
              ajax: '/locations/data',
              &quot;lengthChange&quot;: false,
              &quot;processing&quot;: true,
              &quot;scrollY&quot;: &quot;400px&quot;,
              &quot;paging&quot;: true,
              language: {
              searchPlaceholder: &quot;Enter Location&quot;,
              search: &quot;&quot;,    
          },
  
          &quot;columnDefs&quot;: [    
              { &quot;searchable&quot;: false, &quot;targets&quot;: [2, 3, 5] },    
              {    
                  &quot;render&quot;: function (data, type, row) {    
                      return (Math.round(data * 100) / 100).toFixed(2);;    
                  }, &quot;targets&quot;: [2, 3]    
              },
              { &quot;type&quot;: &quot;airport-first&quot;, &quot;targets&quot;: 1 }    
          ],    
          searchDelay: 350,    
          &quot;pageLength&quot;: 100,    
          &quot;dom&quot;: '&lt;fi&lt;tr&gt;&lt;&quot;bottom&quot;p&gt;&gt;',
  
          &quot;language&quot;: {    
              &quot;processing&quot;: &quot;&lt;i class='fa fa-cog fa-spin fa-3x fa-fw'&gt;&lt;/i&gt;&lt;span class='sr-only'&gt;Loading...&lt;/span&gt;&quot;,    
          }    
      });    
      return table; 
  </code></pre>
  <p>}</p>
  <pre><code>   $.fn.dataTable.ext.type.order['airport-first-asc'] = function (ac) {
          //somehow get data and return if starts with Y?            
          if (ac.startsWith(&quot;Y&quot;)) {
              return 2;
          }
          return 1;
      };
      $.fn.dataTable.ext.type.order['airport-first-desc'] = function (ac) {
              //return everything else that doesn't start with Y??
          if (ac.startsWith(&quot;Y&quot;)) {
              return 1;
          }
          return 2;        
      };
  </code></pre>
  <p>Edit #1:</p>
  <p>How do I connect $.fn.dataTable.ext.type.order with the Search function in the table? This is only activated when I click on the table header column...
  what should these fn's be returning? What does 1 and 2 mean as in <a href="https://datatables.net/examples/plug-ins/sorting_manual.html?" rel="nofollow noreferrer">https://datatables.net/examples/plug-ins/sorting_manual.html?</a>?</p>
  <p>Thank you!</p>
  <p>Data to look as below (cut it down to 2 columns, Filter on column 1 (name) and order on Column 2 codes starting with Y to go to the top of the filtered list).
  so search on sunshine should result in:</p>
  <pre><code>SUNSHINEA   YBSS
  SUNSHINEA   SWQQ
  SUNSHINEA   WSQQ
  SUNSHINEA   TTWW
  </code></pre>
  <p>Html:</p>
  <pre><code>&lt;input id=&quot;searchInput&quot; value=&quot;Type To Filter&quot;&gt;
  &lt;br/&gt;
  &lt;table&gt;
      &lt;thead&gt;
         
      &lt;/thead&gt;
      &lt;tbody id=&quot;fbody&quot;&gt;
          &lt;tr&gt;
              &lt;td&gt;ABERDEEN&lt;/td&gt;
              &lt;td&gt;ADSS&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;ABERDEEN&lt;/td&gt;
              &lt;td&gt;AWAS&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;ABERDEEN&lt;/td&gt;
              &lt;td&gt;TEEE&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;ABERDEEN&lt;/td&gt;
              &lt;td&gt;YABN&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;ABERDEEN&lt;/td&gt;
              &lt;td&gt;ZXSS&lt;/td&gt;
          &lt;/tr&gt;
           &lt;tr&gt;
              &lt;td&gt;SUNSHINEA&lt;/td&gt;
              &lt;td&gt;SWQQ&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;SUNSHINEA&lt;/td&gt;
              &lt;td&gt;WSQQ&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;SUNSHINEA&lt;/td&gt;
              &lt;td&gt;TTWW&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;SUNSHINEA&lt;/td&gt;
              &lt;td&gt;YBSS&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>