Skip to content

Instantly share code, notes, and snippets.

@npearce
Last active June 26, 2024 15:50
Show Gist options
  • Save npearce/ae04e54ce090ed76d1c01770de1cfc10 to your computer and use it in GitHub Desktop.
Save npearce/ae04e54ce090ed76d1c01770de1cfc10 to your computer and use it in GitHub Desktop.

Revisions

  1. npearce revised this gist Mar 24, 2020. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions sort_array_of_JSON_objects.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,18 @@
    // Sort array of JSON objects by date value
    const records = [
    {
    order_id: 12345,
    order_date: "2020-03-23"
    },
    {
    order_id: 12346,
    order_date: "2020-03-20"
    },
    {
    order_id: 12347,
    order_date: "2020-03-22"
    }
    ]

    records.sort((a, b) => {
    return new Date(a.order_date) - new Date(b.order_date); // descending
  2. npearce created this gist Mar 24, 2020.
    9 changes: 9 additions & 0 deletions sort_array_of_JSON_objects.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // Sort array of JSON objects by date value

    records.sort((a, b) => {
    return new Date(a.order_date) - new Date(b.order_date); // descending
    })

    records.sort((a, b) => {
    return new Date(b.order_date) - new Date(a.order_date); // ascending
    })