0

I should sort a multidimensional object, according to the index of a dimension, but also keeping the corresponding indices.

ACTUAL OBJECT STRUCTURE

object1[index1][index2][index3][index4][index5]

SORTING PRIORITY AT PRESENT (OBIVIOUSLY)

1st
[index1]
2nd
[index2]
3rd
[index3]
4th
[index4]
5th
[index5]

EXAMPLE (SORTING AT PRESENT)

1st - object1['1']['50']['0']['0.00']['ARTICOLO3']
2nd - object1['1']['51']['0']['0.00']['ARTICOLO2']
3rd - object1['1']['52']['0']['0.00']['ARTICOLO1']

RE-SORTING EXPECTED

1st
[index4]
2th
[index5]
3rd
[index3]
4th
[index1]
5th
[index2]

EXAMPLE (RE-SORTING BY INDEX5)

1st - object1['1']['52']['0']['0.00']['ARTICOLO1']
2nd - object1['1']['51']['0']['0.00']['ARTICOLO2']
3rd - object1['1']['50']['0']['0.00']['ARTICOLO3']
Davide Cavallini
  • 158
  • 2
  • 13
  • 2
    Javascript objects don't have any order. You need to use an array if order matters. – Barmar Sep 06 '16 at 16:24
  • 1
    do you have an object containing nested objects or an array of objects? Any chance to get the original piece of code including the input object? – briosheje Sep 06 '16 at 16:29
  • Use `Array.prototype.sort` with a comparison function that compares the index that you want. – Barmar Sep 06 '16 at 16:43
  • What is the actual data structure? I have never heard of a multi-dimensional object before. – epascarello Sep 06 '16 at 16:44
  • 1
    The ordering of objects in a JS object is unreliable (there is a lot to it). So what you think is "the current order" of your object might not always be true. See here http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – BadHorsie Sep 06 '16 at 17:04
  • @briosheje probably you have reason. it should be a nested object, and i need to re-sort it according to various necessity. – Davide Cavallini Sep 07 '16 at 07:30
  • @DavideCaballeros: It would be nice to share such an object or the source of it, else it will be quite hard to help you, because as long as we don't have a proper source it's pretty senseless to spend time writing code that may be useless to you ;) – briosheje Sep 08 '16 at 07:30
  • @epascarello you can look the actual structure at "EXAMPLE (SORTING AT PRESENT)" – Davide Cavallini Sep 08 '16 at 12:30
  • That is not valid JavaScript, what is the actual JavaScript code for the object/data. I am trying to determine if it is an array with objects, or an object with objects. Depending on that, there are two answers... – epascarello Sep 08 '16 at 13:24

0 Answers0