title: Laravel return data by sort
body: <p>I have problem to returning json data and <code>sort</code> them in <code>laravel</code>, they just appear randomly.</p>
  
  <h1>What I did so far</h1>
  
  <ol>
  <li><p>I tried to return data by their <code>id</code> from database and help of
  JavaScript like:</p>
  
  <p><code>result.sort(function(a,b) {
    return (a.id &gt; b.id) ? 1 : ((b.id &gt; a.id) ? -1 : 0);
  });</code>
  Result was random order (sort)</p></li>
  <li><p>I added <code>sort</code> column to my database and tried to get my data base on numbers I provided there (result was random order)</p></li>
  <li><p>I tried to add <code>-&gt;orderByRaw('set_specification.sort')</code> in my function code and get orders by that (result was random order)</p></li>
  </ol>
  
  <h1>Logic</h1>
  
  <ol>
  <li>I select a <code>set</code></li>
  <li><code>Set</code> child's will appear on blade by <code>sort</code> column order which I
  provide numbers in it.</li>
  </ol>
  
  <h1>Code</h1>
  
  <p><code>controller</code></p>
  
  <pre><code>public function selectset($id){
        $selectsets = DB::table('sets')
          -&gt;where('sets.id', '=', $id)
          -&gt;join('set_specification', 'sets.id', '=', 'set_specification.set_id')
          -&gt;join('specifications', 'set_specification.spec_id', '=', 'specifications.id')
          -&gt;orderByRaw('set_specification.sort')
          -&gt;get();
        return response()-&gt;json($selectsets);
      }
  </code></pre>
  
  <p><code>JavaScript</code></p>
  
  <pre><code>$(document).ready(function() {
      $('select[name="selectset"]').on('change', function() {
        var id = $(this).val();
        if(id) {
        $.ajax({
          url: '{{ url('admin/selectset') }}/'+encodeURI(id),
          type: "GET",
          dataType: "json",
          success:function(result) {
  
  
            result.sort(function(a,b) {
              return (a.id &gt; b.id) ? 1 : ((b.id &gt; a.id) ? -1 : 0);
            });
  
            console.log(result);
  
  //rest of code..
  }
  </code></pre>
  
  <h1>Issues</h1>
  
  <p>Even when I return my data by help of ajax sort (as you see in my code) in console it returns correctly but in blade it appears as it wish!</p>
  
  <p><a href="https://i.stack.imgur.com/oWCZw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oWCZw.png" alt="screenone"></a></p>
  
  <p>in case image above won't work <a href="https://ibb.co/cj8qJU" rel="nofollow noreferrer">here it is again</a></p>
  
  <h1>Question</h1>
  
  <ol>
  <li>How can I fix this sorting issue?</li>
  </ol>
  
  <h1>Update</h1>
  
  <p>I changed my ajax code to:</p>
  
  <pre><code>result.sort(function(a,b) {
    return (a.sort &gt; b.sort) ? 1 : ((b.sort &gt; a.sort) ? -1 : 0);
  });
  </code></pre>
  
  <p>and result is the same <a href="https://ibb.co/hKzVJU" rel="nofollow noreferrer">screenshot</a></p>