2

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!

1
  • why have you kept date as key Commented Aug 7, 2018 at 20:58

4 Answers 4

4

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; }

Sign up to request clarification or add additional context in comments.

2 Comments

That's it! Thank you so much!
Since lodash is allowed (question is tagged with it) it seems you could just do _.sortBy(array, (x) => new Date(_.head(_.keys(x))))
1

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.

Comments

1

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.

2 Comments

I went ahead and reformatted it the way you suggested to hopefully avoid any future hiccups. Thanks!
Do you understand why though? That is even more important.
0

This is one way to do it in a very concise way using lodash sortBy / head / keys:

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" } } ];

console.log(_.sortBy(array, (x) => new Date(_.head(_.keys(x)))));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

I personally think since lodash is allowed in this question this is cleaner one liner solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.