-1

I have a problem sorting this nested objects; the object I have is:

Array of objects: [Object, Object]

inside this array there are two objects and inside those objects are 1 object: 2016-5:Object 2016-6:Object

Inside every of the two objects are arrays with only 1 number inside:

shift0:Array[1]
shift1:Array[1]
shift2:Array[1]
shift3:Array[1]

and inside the array is only one number like so (all with index 0 of the array):

shift0:Array[1] -> 26
shift1:Array[1] -> 42
shift2:Array[1] -> 53
shift3:Array[1] -> 31

I want to be able to sort the numbers - 26, 42, 53, 31

so it looks like

var source = [{'2016-5': [{shift0: [26]}, 
                       {shift1: [42]}, 
                       {shift2: [53]}, 
                       {shift3: [31]}]},
           {'2016-6':  [{shift0: [33]}, 
                        {shift1: [15]}, 
                        {shift2: [13]}, 
                        {shift3: [66]}]}
 ];

the result i want should be:

var source = [{'2016-5': [{shift0: [26]}, 
                           {shift3: [31]}, 
                           {shift1: [42]}, 
                           {shift2: [53]}]},
               {'2016-6':  [{shift2: [13]}, 
                            {shift1: [15]}, 
                            {shift0: [33]}, 
                            {shift3: [66]}]}
     ];
Dimitar
  • 1,830
  • 5
  • 32
  • 52
  • The example you give is not valid JSON: `{2016-5: {shift0: [26]}, {shift0: [42]}}`. Moreover, can you add the expected result on this example? – dgiugg May 13 '16 at 10:15

2 Answers2

1

try this

var source =  [{'2016-5': [{shift0: [26]}, 
                       {shift1: [42]}, 
                       {shift2: [53]}, 
                       {shift3: [31]}]},
           {'2016-6':  [{shift0: [33]}, 
                        {shift1: [15]}, 
                        {shift2: [13]}, 
                        {shift3: [66]}]}
 ];

source.forEach( function(obj){
   Object.keys(obj).forEach( function(key){
      console.log(obj[key]);
      obj[key].sort( function(a,b){
         return a[ Object.keys(a)[0] ]  - b[ Object.keys(b)[0] ] ;
      })
   });
});

document.body.innerHTML += JSON.stringify(source, 0, 4 );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • thanks for ur answer im getting `TypeError: obj[key].sort is not a function` but i will debug the code i think the problem is on my end. :) – Dimitar May 13 '16 at 13:36
  • my var is a little different then what i wrote and this code its not working and im very bad at sorting things out: `[{"2016-5":{"shift0":[26],"shift1":[42],"shift2":[53],"shift3":[31]}},{"2016-6":‌​{"shift0":[47],"shift1":[26],"shift2":[58],"shift3":[52],"shift4":[22]}}]` – Dimitar May 14 '16 at 06:47
  • @ДимитърКлатуров It is totally different. In your question `2016-5` has array value but in your comment it has an object. It totally invalidates mine and other answers. – gurvinder372 May 14 '16 at 06:51
  • yes i know, sorry about that, its my mistake, but i will be grateful if you can help me sort as it is in the comment. :-/ – Dimitar May 14 '16 at 06:56
  • @ДимитърКлатуров actually you can't sort the JSON object, since keys don't follow any order like array items. Have a look http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value – gurvinder372 May 14 '16 at 07:02
0

The JSON you gave is invalid, assuming your JSON as below:

var source = [{'2016-5': [{shift0: [26]}, 
                           {shift0: [42]}, 
                           {shift0: [53]}, 
                           {shift0: [31]}]},
               {'2016-6':  [{shift0: [33]}, 
                            {shift0: [15]}, 
                            {shift0: [13]}, 
                            {shift0: [66]}]}
 ];

Then you could try :

var result =
         source.map(function(item){
                    Object.keys(item)
                          .map(function(key){
                                 item[key] = 
                                      item[key].sort(function(p, c){
                                             return p.shift0[0]-c.shift0[0];
                                      });
                           });
                     return item;
         });
Dhananjaya Kuppu
  • 1,322
  • 9
  • 10
  • can u please edit your answer as i edited my question and i have really mistaken the json that i have and the json have inside different values of shift0, shift1, shift2... to a certain number (i dont know how many shifts there will be). thanks! – Dimitar May 13 '16 at 13:07