There is such an object:

data['params']['last_name']['property_xxc'] = 'abcd'; data['params']['last_name']['sort_index'] = 2; data['params']['name']['property_erw'] = 'abcd'; data['params']['name']['sort_index'] = 5; data['params']['date']['property_xxc'] = 'abcd'; data['params']['date']['sort_index'] = 3; 

This object with such a deep nesting, you need to sort by the floor sort_index , in the end you need to get such an object:

 data['params']['name']['property_erw'] = 'abcd'; data['params']['name']['sort_index'] = 5; data['params']['date']['property_xxc'] = 'abcd'; data['params']['date']['sort_index'] = 3; data['params']['last_name']['property_xxc'] = 'abcd'; data['params']['last_name']['sort_index'] = 2; 

I tried to sort using sort() like this:

 var compareData = (a, b) =>{ return a.sort_index - b.sort_index; }; 

But nothing happened. Tell me how to do this sorting?

  • And why did you decide that your object will be sorted by the sort method? - Dmitriy Simushev
  • those. It is necessary to change the order of properties in the object. what is it for? - Stack
  • You were also told that sorting fields in the object does not make sense, and sorting the output of JSON.stringify not guaranteed. If you need guaranteed sorting, why not change the data format to sortable? - Nick Volynkin
  • Your first and second objects are identical. Sorting the text view is unstable and can ever lead to a floating bug. - Nick Volynkin

2 answers 2

If you need to change the order of the properties in the object and in the javascript code there are the following lines

 data['params']['last_name']['property_xxc'] = 'abcd'; data['params']['last_name']['sort_index'] = 2; data['params']['name']['property_erw'] = 'abcd'; data['params']['name']['sort_index'] = 5; data['params']['date']['property_xxc'] = 'abcd'; data['params']['date']['sort_index'] = 3; 

then this means that the following object is in memory:

 var data = { 'params': { 'last_name': { 'property_xxc': 'abcd', 'sort_index': 2 }, 'name': { 'property_erw': 'abcd', 'sort_index': 5 }, 'date': { 'property_xxc': 'abcd', 'sort_index': 3 } } }; 

Check it out easily. For example, so

 try { data['params']['last_name']['property_xxc'] = 'abcd'; } catch(e) { alert(e); } // alert не появится, значит все правильно 

If you need to change the order of the properties in this object, you can do this:

 var result = { 'params': {} }; Object.keys(data.params).sort(function (a, b) { return data.params[b].sort_index - data.params[a].sort_index; }).forEach(function (v) { result.params[v] = data.params[v]; }); 

In result we get the properties in the required order. This can be checked by calling JSON.stringify(result) , we get

 { "params": { "name": { "property_erw": "abcd", "sort_index": 5 }, "date": { "property_xxc": "abcd", "sort_index": 3 }, "last_name": { "property_xxc": "abcd", "sort_index": 2 } } } 

Although this can be done, it is usually not necessary to change the order of properties in the object, but it may be necessary, for example, for code generation. For example, if you need to serialize an object into a JSON file that will be edited manually (for example, in Visual Studio 2015, the project.json configuration files are used).

  • 6
    since the specification does not guarantee the order of crawling the properties in the object, this implementation will depend on the browser in which it starts, since any browser can use any order of crawling the fields of the object. - Grundy
  • @Grundy "this implementation will depend on the browser" - checked in Edge. works. but I still do not understand why this may be needed? if for data output, you need to sort the data, not the properties in the object. - Stack
  • 2
    The fact that somewhere this code works does not guarantee that it will work everywhere and always . The only thing that does not change is the specification, but she says that the order of properties is not guaranteed - Dmitriy Simushev
  • @DmitriySimushev "The fact that somewhere this code works does not guarantee that it will work everywhere and always." - Yes, nothing lasts forever. but we will not generalize. there is a question. and although it is not clear what it is for, but nevertheless, I have saved all levels of nesting in the answer, and what is needed is done. and once again I want to draw attention to my second question: why do we need to sort the order of properties in an object? my answer is: don't do this. - Stack
  • @Grundy, ES6 guaranteed. But there is one thing about int32-keys. In general, if all keys are non-numeric, then in all browsers (even old ones) the order will be the same. If there are numeric ones, then it is fundamentally impossible to sort. - Qwertiy

Your data structure is not an array, but an object . The order of the elements in the object is not guaranteed by the specification (at least until ES2015 and even there are subtleties with integer keys), therefore, in general, its sorting does not make sense. An attempt to call the sort method on an object will fail, because it simply does not have such a method.

As a result, if you want to sort your data, then you should first go from object to array, and only then sort by using Array.prototype.sort . This can be done, for example, as follows:

 var data = { 'params': [ { 'name': 'last_name', 'property_xxc': 'abcd', 'sort_index': 2 }, { 'name': 'name', 'property_erw': 'abcd', 'sort_index': 5 }, { 'name': 'date', 'property_xxc': 'abcd', 'sort_index': 3 } ] } data.params.sort(function(a, b) { return a.sort_index - b.sort_index; }); console.dir(data); 
  • @Stack, most likely the code in question works, and also most likely it is php. so the answer really doesn’t belong at all, since the answer shows an example of the structure you need - Grundy
  • @Grundy "most likely this is php." - In the question tag [javascript]. look at my answer. added an explanation to the beginning. - Stack
  • @Stack, I need an author comment. In any case, the answer indicates the structure with which you need to work instead of the one indicated in the question. Thus, your first comment tore out the line from the question of filling the object, and most likely it just shows what the specific field is equal to, since the author did not guess json to put it out, and also it does not indicate what kind of data it is and where it turns out, and I'm more and more confident that this is exactly an example of filling in php and not in a javascript, and trying to apply it to another structure specified in the answer, this will not work, because the structure is changed and already filled. - Grundy
  • one
    Ваша структура данных - это не массив, а объект. There is no talk about arrays in the topic. скорее всего код в вопросе работает, а также скорее всего это php tags, see what language does the PS refer to in the array that is given in this answer, the structure that is in the header is corrupted - sanu0074
  • one
    @ sanu0074, yes I saw that there is no mention of the array. But the order of the keys in the object is not guaranteed, therefore it is impossible to talk about sorting - Dmitriy Simushev