How to sort a JS object object?

I built the object in PHP, used the JSON_encode function, and sent it as a JSON string in a JS script via ajax. Then I convert it back to an object. The problem I am facing is that I wanted to keep the object in the order in which it was originally created. Please look at this image of how the object looks as soon as I get into JS:

enter image description here

When I created the object, it was sorted by client field in alphabetical order. The customer name starting with A will be first, B second, etc. As you can see, now the first element of the object is as a client, starting with S. It seems that it is somehow sorted automatically by the top-level key of the object, which is an integer, so I understand why this happened.

So what I want to do is re-sort this object so that all sub-objects are sorted alphabetically by customer field. Is it possible? If so, how to do it?

Thanks!

+4
source share
4 answers

This is probably the difference between a JavaScript object and a JavaScript array. Objects are more like hash tables where keys are not sorted in any particular order, while arrays are linear collections of values.

At the end, make sure you encode the array, not the object. Check the final encoded JSON, and if your collection of objects is surrounded by {} instead of [], it will be encoded as an object, not an array.

You may have a problem, since it looks like you are trying to access objects by ID number and that the index you want these objects to occupy in the final array is another problem, because you probably don't want an array with 40,000 entries when you only save a small amount of values.

If you just want to iterate over objects, you have to make sure that you encode the array instead of the object. If you want to access objects by a specific identifier, you probably have to sort the objects on the client side (i.e., have the object from the JSON response, and then create another array and sort these objects in it so that you can have sorted objects and still have access to them by id).

With Google, you can easily find efficient sorting algorithms (or use one of them from ELCas).

+2
source

I modified Fabricio Matée to become more flexible and return a sorted object.

 function alphabetical_sort_object_of_objects(data, attr) { var arr = []; for (var prop in data) { if (data.hasOwnProperty(prop)) { var obj = {}; obj[prop] = data[prop]; obj.tempSortName = data[prop][attr].toLowerCase(); arr.push(obj); } } arr.sort(function(a, b) { var at = a.tempSortName, bt = b.tempSortName; return at > bt ? 1 : ( at < bt ? -1 : 0 ); }); var result = []; for (var i=0, l=arr.length; i<l; i++) { var obj = arr[i]; delete obj.tempSortName; for (var prop in obj) { if (obj.hasOwnProperty(prop)) { var id = prop; } } var item = obj[id]; result.push(item); } return result; } 

Then just call a function like this

 your_object = alphabetical_sort_object_of_objects(your_object, 'attribute_to_sort'); 
+5
source

Here is a general iterative function that pushes all objects into an array and sorts them according to the customer property in a case-insensitive manner, then iterates over the sorted array:

 function iterate(data) { var arr = []; for (var prop in data) { if (data.hasOwnProperty(prop)) { var obj = {}; obj[prop] = data[prop]; obj.tempSortName = data[prop].customer.toLowerCase(); arr.push(obj); } } arr.sort(function(a, b) { var at = a.tempSortName, bt = b.tempSortName; return at > bt ? 1 : ( at < bt ? -1 : 0 ); }); for (var i = 0, l = arr.length; i < l; i++) { var obj = arr[i]; delete obj.tempSortName; console.log(obj); for (var prop in obj) { if (obj.hasOwnProperty(prop)) { var id = prop; //gets the obj "index" (id?) } } console.log(id); var item = obj[id]; console.log(item.customer); //do stuff with item } } 

Fiddle

+1
source

Just scroll through your data and sort them. I do not use the most efficient sorter, but there are ideas.

 var data = toArray(AJAX_Returned_JSON); // function to convert object to array var sortedData = null; for(var i=0; i<data.length - 1; i++){ var temp = data[i]; for(var j=i+1; j<data.length; j++){ if(data[j].customer < temp.customer){ temp = data[j]; } } sortedData.push(temp); } 
-1
source

All Articles