Browser automatically sorting json object based on key problem..

Json data got sorted on browser by its key

This a problem where the browser decides to sort the json you provided its own way rather than the one you sorted.

And it happens especially if you have key value pair in json payload you are passing.

So the scenario would be, there JSON data you are sending from the backend is sorted and you displayed it on front end with some javascript, but the result you are showing is not sorted with the original sorting order you set.

Solution

I observed that some browsers, at least Chrome, is doing the sorting in the situation that is discussed above.

For the fix: make the key of the json values you are passing string format rather than number

like

in place of using:
{1:"one", 4:"four"}

use

{"1":"one", "4":"four"}

error = MongoId not found in symfony application

Using unquoted json formatted result set in symfony2 twig

error duplicate entry for key when creating new unique key mysql

gem install json -v ‘1.8.1’ error on rails installing fedora

Browser shows the same image after the update

Sorting array by two fields in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*