CakePHP with a query string /api/parts/getParts?sort=sort_order&direction=asc&limit=100

Returns items out of order. See below for the work-a-round


{
"parts": [
{
"sort_order": 10
},
{
"sort_order": 20
},
{
"sort_order": 30
},
{
"sort_order": 40
},
{
"sort_order": 60
},
{
"sort_order": 70
},
{
"sort_order": 120
},
{
"sort_order": 130
},
{
"sort_order": 140
},
{
"sort_order": 150
},
{
"sort_order": 160
},
{
"sort_order": 170
},
{
"sort_order": 200
},
{
"sort_order": 50
},
{
"sort_order": 62
},
{
"sort_order": 72
},
{
"sort_order": 80
},
{
"sort_order": 90
},
{
"sort_order": 110
}
]
}

I don’t get this because the sort_order database field is an int(11) and well an integer should sort numerically.

So this is the work-a-round but still no idea why it’s needed

<br />
// api/PartsController.php</p>
<p>public function getParts() {</p>
<p>        $parts = $this-&gt;Parts-&gt;find('all', [<br />
            'fields' =&gt; ['sort_order'],<br />
            'conditions' =&gt; [<br />
                'active' =&gt; true,<br />
                'NOT' =&gt; [<br />
                    'co_visit' =&gt; true,<br />
                ],<br />
            ],<br />
            'order' =&gt; [<br />
                'sort_order + 0', // this is the magic the + 0 forces it to sort numerically<br />
            ]<br />
        ]<br />
        );</p>
<p>        $this-&gt;set(compact('parts'));<br />
        $this-&gt;set('_serialize', ['parts']);<br />
    }</p>
<p>

And the result

{
"parts": [
{
"sort_order": 10
},
{
"sort_order": 20
},
{
"sort_order": 30
},
{
"sort_order": 40
},
{
"sort_order": 50
},
{
"sort_order": 60
},
{
"sort_order": 62
},
{
"sort_order": 70
},
{
"sort_order": 72
},
{
"sort_order": 80
},
{
"sort_order": 90
},
{
"sort_order": 110
},
{
"sort_order": 120
},
{
"sort_order": 130
},
{
"sort_order": 140
},
{
"sort_order": 150
},
{
"sort_order": 160
},
{
"sort_order": 170
},
{
"sort_order": 200
}
]
}