JavaScript Sort Array of Nested Objects by Dynamic Key sort nested array javascript,sort nested array php,sort nested

I believe I'm wording this question correctly.... I have a dataset like this...

[
  {
    "Fri Aug 03 2018 00:00:00 GMT-0500": {
      "bill": "Test 2",
      "account": "defaultAccount",
      "amount": 40,
      "category": "defaultCategory",
      "description": "231"
    }
  },
  {
    "Fri Aug 17 2018 00:00:00 GMT-0500": {
      "bill": "Test 2",
      "account": "defaultAccount",
      "amount": 40,
      "category": "defaultCategory",
      "description": "231"
    }
  },
  {
    "Wed Aug 01 2018 00:00:00 GMT-0500": {
      "bill": "test",
      "account": "create-account",
      "amount": 40,
      "category": "mortgage",
      "description": "23"
    }
  }, ....
]

and I'm struggling to sort the array based on the object's dynamic date. I've spent the last few hours going through every version of this question on stack overflow and I'm not coming up with a solution... any advice is greatly appreciated!

Answer:1

You need to get the key of each outer object and get a date object. Then take the delta for sorting.

var array = [{ "Fri Aug 03 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Fri Aug 17 2018 00:00:00 GMT-0500": { bill: "Test 2", account: "defaultAccount", amount: 40, category: "defaultCategory", description: "231" } }, { "Wed Aug 01 2018 00:00:00 GMT-0500": { bill: "test", account: "create-account", amount: 40, category: "mortgage", description: "23" } }];

array.sort((a, b) => new Date(Object.keys(a)[0]) - new Date(Object.keys(b)[0]));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:2

I will do like below using sortBy function:

import _ from "lodash";

var array = [
  {
    "Fri Aug 03 2018 00:00:00 GMT-0500": {
      bill: "Test 2",
      account: "defaultAccount",
      amount: 40,
      category: "defaultCategory",
      description: "231"
    }
  },
  {
    "Fri Aug 17 2018 00:00:00 GMT-0500": {
      bill: "Test 2",
      account: "defaultAccount",
      amount: 40,
      category: "defaultCategory",
      description: "231"
    }
  },
  {
    "Wed Aug 01 2018 00:00:00 GMT-0500": {
      bill: "test",
      account: "create-account",
      amount: 40,
      category: "mortgage",
      description: "23"
    }
  }
];

const result = _.sortBy(array, [
  function(o) {
    return new Date(Object.keys(o)[0]);
  }
]);

console.log(result);

Codesanbox link.

Answer:3

Try formatting your objects a little differently, like this:

[{
    "createdon": "Fri Aug 03 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"
  },
  {
    "createdon": "Fri Aug 17 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"

  },
  {
    "createdon": "Wed Aug 01 2018 00:00:00 GMT-0500",
    "bill": "test",
    "account": "create-account",
    "amount": 40,
    "category": "mortgage",
    "description": "23"
  }
]

This is more logical because the index in the array is already the key which identifies each object so you don't need to use the date as a key.

Here is a complete example:

var data = [{
    "createdon": "Fri Aug 03 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"
  },
  {
    "createdon": "Fri Aug 17 2018 00:00:00 GMT-0500",
    "bill": "Test 2",
    "account": "defaultAccount",
    "amount": 40,
    "category": "defaultCategory",
    "description": "231"

  },
  {
    "createdon": "Wed Aug 01 2018 00:00:00 GMT-0500",
    "bill": "test",
    "account": "create-account",
    "amount": 40,
    "category": "mortgage",
    "description": "23"
  }
]

result = _.sortBy(data, [
  function(o) {
    return new Date(o.createdon);
  }
])

Even though the other answers work, this method should result in cleaner, clearer and more maintainable code which is also a very important part of programming.

Answer:4

My website allow users to signup by their Google or Facebook account. But some users use clone account to signup for spamming, so I want to detect and allow only Facebook or Google account which was ...

My website allow users to signup by their Google or Facebook account. But some users use clone account to signup for spamming, so I want to detect and allow only Facebook or Google account which was ...

I'm trying to figure out why a call to this.setState isn't updating state. I have the following lines method: changeState(state, value){ this.setState({state:value}, ()=>console.log(this....

I'm trying to figure out why a call to this.setState isn't updating state. I have the following lines method: changeState(state, value){ this.setState({state:value}, ()=>console.log(this....

  1. react this setstate is not a function
  2. react this setstate callback
  3. react this setstate not working
  4. react this setstate prevstate
  5. react this.setstate
  6. react this.setstate example
  7. react this.setstate undefined
  8. react this.setstate not updating state
  9. react this.setstate then
  10. react this.setstate async
  11. react this.setstate array
  12. react this.setstate not updating
  13. react this.setstate does not work
  14. react this.setstate function
  15. react this.setstate promise
  16. react this.setstate object
  17. react this.setstate in constructor
  18. react this.setstate in render
  19. react this.setstate multiple
  20. react this.setstate is not defined

Currently creating an application using Angular, socket.io and express. However, I run in an async issue which I have a hard time getting a solution for. Here is the code: export class ...

Currently creating an application using Angular, socket.io and express. However, I run in an async issue which I have a hard time getting a solution for. Here is the code: export class ...

I have 5 high charts on my page and I need series label in only one of the charts. But the moment I include series-label.js , it adds series label to all charts. Also how do I change the color of ...

I have 5 high charts on my page and I need series label in only one of the charts. But the moment I include series-label.js , it adds series label to all charts. Also how do I change the color of ...