0

I need to sort Date in object (as key).

I think that I should to convert it in an array of objects and after I have to convert it into an object, but it seems too difficult. This is the data of the multilevel object:

{
  "2017-02-18": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 13.4,
      "provider": 13.04
    }
  },
  "2017-02-14": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 5.4,
      "provider": 5.04
    }
  },
  "2017-02-13": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 1.4,
      "provider": 1.04
    }
  },
  "2017-02-17": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 21.4,
      "provider": 21.04
    }
  },
  "2017-02-15": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 44.4,
      "provider": 44.04
    }
  },
  "2017-02-16": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 56.4,
      "provider": 56.04
    }
  }
}

After the sort I need an object with this result:

{
  "2017-02-13": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 1.4,
      "provider": 1.04
    }
  },

  "2017-02-14": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 5.4,
      "provider": 5.04
    }
  },
  "2017-02-15": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 44.4,
      "provider": 44.04
    }
  },
  "2017-02-16": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 56.4,
      "provider": 56.04
    }
  },
  "2017-02-17": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 21.4,
      "provider": 21.04
    }
  },
  "2017-02-18": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 13.4,
      "provider": 13.04
    }
  }
}

Thanks to all for help!

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Diegs
  • 21
  • 3
  • See http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key. It is not possible to sort object keys. You could sort the keys though. – dev8080 Apr 05 '17 at 09:06

1 Answers1

0

Citing this topic, I added the date sort function:

var datesObj = {
  "2017-02-18": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 13.4,
      "provider": 13.04
    }
  },
  "2017-02-14": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 5.4,
      "provider": 5.04
    }
  },
  "2017-02-13": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 1.4,
      "provider": 1.04
    }
  },
  "2017-02-17": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 21.4,
      "provider": 21.04
    }
  },
  "2017-02-15": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 44.4,
      "provider": 44.04
    }
  },
  "2017-02-16": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 56.4,
      "provider": 56.04
    }
  }
};

var datesArr =[];
for(var dateKey in datesObj)
  datesArr.push(dateKey);  
  
datesArr.sort(function(dt1, dt2){
        return (new Date(dt1)).getTime() -(new Date(dt2)).getTime();
   });
   
   for (i = 0; i < datesArr.length; i++) {
  
         alert(datesArr[i] + ':' + datesObj[datesArr[i]]);
}
Community
  • 1
  • 1
dev8080
  • 3,950
  • 1
  • 12
  • 18
  • Thanks for help ;) But how to return the same object (and not the alert)? – Diegs Apr 05 '17 at 10:58
  • Please read the cited link in my answer. It is not possible to sort javascript Object keys. I just provided an answer to sort dates that are object keys after which you can decide what to do with them. I think you should use the sorted keys to access their values. – dev8080 Apr 05 '17 at 19:11