Hello guys,
I have a question about sorting json response returned by the controller.
I have an action that returns an array sorted by value like this:
[
"3" => "a",
"2" => "b",
"1" => "c"
]
The array is sorted by the letter as expected. But when I receive the response in the browser, it is sorted by the number, like this:
[
"1" => "c",
"2" => "b",
"3" => "a",
]
How can I force the sorting to be by value?
Thanks.
This is how I quit the action:
return $this->asJson($sortedArray);
Looks like the response unsort it.
Anyone knows what I’m doing wrong? Is it a bug?
Hi,
Just sort the php array before returning…
The following snippet may help you!
ksort($sortedArray); //Use this, If your array is associative array
return $this->asJson($sortedArray);
In my case asort() would be more appropriate since I want to order by value not by key. But it doesn’t work.
My array is already sorted the way I want before calling (I’ve put a breakpoint):
return $this->asJson($sortedArray);
The asJson() function seems to mess the order.