title: Laravel Datatables: How to sort column with the second data
body: <p>How do I sort according to the second data? The sorting works only on the ID. But in my table, I added a badge count and that's what I want to sort by.</p>
  <p>Here is my controller</p>
  <pre class="lang-php prettyprint-override"><code>    return DataTables::of($users)
          -&gt;addIndexColumn()
          -&gt;addColumn('Inventories', function ($row) {
              if($row-&gt;status != 'Dumped2') {
                  return '&lt;a href=&quot;'.route('admin.clients.show-client-inventories2', $row-&gt;id).'&quot; class=&quot;btn btn-info btn-sm&quot;&gt;'.__(&quot;Inventories&quot;).' &lt;span class=&quot;right badge badge-success&quot; title=&quot;Total Lost&quot;&gt;'.(abs($row-&gt;inv_total_lost_qty) ?? 'N/A').'&lt;/span&gt; &lt;/a&gt;';
              }
          })
          -&gt;addColumn('Returns', function ($row) {
              if($row-&gt;status != 'Dumped2') {
                  return ' &lt;a href=&quot;'.route('admin.clients.show-client-returns', $row-&gt;id) .'&quot; class=&quot;btn btn-info btn-sm&quot;&gt;'.__('Returns').' &lt;span class=&quot;right badge badge-warning&quot; title=&quot;Total Returns&quot;&gt;'.(abs($row-&gt;overall_returns_count) ?? 'N/A').'&lt;/span&gt; &lt;/a&gt;';
              }
              $row-&gt;status-&gt;orderBy(abs($row-&gt;overall_returns_count));
          })
          -&gt;addColumn('inbound_shipments', function ($row) {
              if($row-&gt;status != 'Dumped2') {
                  return '&lt;a href=&quot;'. route('admin.clients.show-client-inbound-shipments', $row-&gt;id).'&quot; class=&quot;btn btn-info btn-sm&quot;&gt;'. __('Inbound Shipments').'&lt;span class=&quot;right badge badge-danger&quot; title=&quot;Total Overcharged Fees&quot;&gt;'. abs($row-&gt;shipment_quantity_diff).'&lt;/span&gt; &lt;/a&gt;';
              }
          })
          -&gt;addColumn('overcharged', function ($row) {
              if($row-&gt;status != 'Dumped2') {
                  return '&lt;a href=&quot;'. route('admin.clients.show-client-overcharged-fee', $row-&gt;id).'&quot; class=&quot;btn btn-info btn-sm&quot;&gt;'.__('Overcharged Fee') .' &lt;span class=&quot;right badge badge-primary&quot; title=&quot;Total Overcharged Fees&quot;&gt;'.(abs($row-&gt;overall_overcharged_fees_count) ?? 'N/A').'&lt;/span&gt; &lt;/a&gt;';
              }
          })
          -&gt;addColumn('ALL', function ($row) {
              if($row-&gt;status != 'Dumped2') {
                  $arr = array(abs($row-&gt;overall_overcharged_fees_count), abs($row-&gt;overall_returns_count), abs($row-&gt;inv_total_lost_qty),abs($row-&gt;shipment_quantity_diff));
                  return array_sum($arr);
              }
          })
          -&gt;addColumn('credentials', function ($row) {
              if($row-&gt;status != 'Dumped2') {
                  return '&lt;button onclick=&quot;set_credentials(this)&quot; class=&quot;btn btn-danger btn-sm&quot;&gt; '.__(&quot;Set Credentials&quot;).'&lt;/button&gt;';
              }
          })
          -&gt;addColumn('reim', function ($row) {
              if($row-&gt;status != 'Dumped2') {
                  return '&lt;a href=&quot;'. route('admin.clients.show-client-reimbursements', $row-&gt;id).'&quot; class=&quot;btn btn-danger btn-sm&quot;&gt;'.__('Reimbursements') .'&lt;/a&gt;';
              }
            
          })
          -&gt;rawColumns(['Inventories','action','Returns','inbound_shipments','overcharged','ALL','credentials','reim'])
          -&gt;make(true);
  }
  
  return view('admin.clients.index', compact('users'));
  </code></pre>
  <p>The column I want to sort is not <code>$row-&gt;id</code>, what I want is the second one like <code>abs($row-&gt;overall_returns_count)</code>.</p>
  <p>And in my Blade code. I wrote something like this:</p>
  <pre class="lang-js prettyprint-override"><code>//Update start
  {width: &quot;10%&quot;, data: 'ALL', name: 'ALL', orderable: true, searchable: false},
  {width: &quot;10%&quot;, data: 'Inventories', name: 'Inventories', orderable: true, searchable: false},
  {width: &quot;10%&quot;, data: 'Returns', name: 'Returns', orderable: true, searchable: false},
  {width: &quot;15%&quot;, data: 'inbound_shipments', name: 'Inbound Shipments', orderable: true, searchable: false},
  {width: &quot;10%&quot;, data: 'overcharged', name: 'Overchared Fee', orderable: true, searchable: false},
  </code></pre>
  <p><a href="https://i.stack.imgur.com/3XgG6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3XgG6.png" alt="enter image description here" /></a></p>
  <p>As you can see, if I click the header, it doesn't sort the badge. Instead, it sorts the <code>id</code> as you can see in the lower left corner when I hovered the button. (id starting in 2)</p>
  <p>System details
  Operating System Windows 10</p>
  <p>PHP Version 7.2</p>
  <p>Laravel Version 6.0</p>
  <p>Laravel-Datatables Version ^9.6</p>