I'm having a hard time sorting my data. Because I am fetching my data using 2 columns. Is there a way where I can sort it in the controller, ajax, model? Any suggestion will be appreciated.

[![enter image description here](https://i.stack.imgur.com/qIGeU.png)](https://i.stack.imgur.com/qIGeU.png)

**As we can see here, I am fetching all data of test 1. But when I'm fetching it, it is not in ascending order.**

My DB:

[![enter image description here](https://i.stack.imgur.com/SiVCn.png)](https://i.stack.imgur.com/SiVCn.png)

**View:**

```
<div id="testdisplay"></div>

```

**Ajax**

```
<script>

testfetching();
function testfetching(){

var eventID = $('#eventID').val();

  $.ajax({
        url:"<?=site_url('testing/fetching')?>",
        method:"POST",
        data:{eventID:eventID},
        dataType:"json",
        success:function(data){

        $('#testdisplay').html(data);

        },
    })

}
</script>

```

**Controller:**

```
    // is there a way where i can sort my data here in controller?
    // I have tried sorting it using sort($data); but it is not working. 

         function fetching(){
        $entry= $this->matchings->entryfetch();

        $data = '';
        $data='

    <div class="card table-hover table-responsive" style="display: flex;">
    <table class="mt-3 ml-2" style="">
    <thead>
    <tr>
    <th style="float: left;">NO.</th>
    <th style="float: left;">ENTRY NAME</th>

    </tr></thead>';

        foreach($entry as $entry){

            $match= $this->matchings->matchfetch($entry->handlerID);

            $data.='<thead>
            <tr>
            <th style="float: left; page-break-after: always;"><hr style="">'.$entry->handlerID.'&nbsp;'.$entry->entryName.'</th><th>&nbsp;</th>  ';

            foreach($match as $match){

                if($match->handlerIDM === $entry->handlerID){
                    $name=$match->handlertestW;
                    $count=$match->cockNoM;
                }else{

                    $name=$match->handlerM;
                    $count=$match->cockNoW;
                }

                if($match->handlerM === $entry->entryName){
                    $data.='<tbody>
<tr><td style="float: right; margin-right: 5px;">'.$count.'</td>

';

                }else{

                    $data.='<tbody><tr> <td style="float: right; margin-right: 5px;">'.$count.'</td>

';

                }

                $data.='<td></td></tr></tbody>';
                //
            }

        }

        $data .='</table></div>';

        echo json_encode($data);

    }

```

**Model:**

```
 function entryfetch(){
        $eventID = $this->input->post('eventID');

        $this->db->where('eventID', $eventID);
        $this->db->group_by('handlerID');
        $this->db->order_by('handlerID');
        $query = $this->db->get('entry_test');

        return $query->result();

    }

    function matchfetch($entryid){

        $eventID = $this->input->post('eventID');

        $this->db->where('eventID', $eventID);
        $this->db->where('handlerIDM', $entryid);

        $this->db->or_where('handlerIDW', $entryid);
        $this->db->where('eventID', $eventID);

        $query = $this->db->get('matching');

        return $query->result();

    }

```