Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
javascript - sort nested json object

I want to sort the following json object in javascript by the "orderby" key (see second json object for the output I require):

{
  "1": {
    "category": "Year",
    "value": "2028",
    "title": "Current Year",
    "description": "",
    "orderby": "3"
  },
  "2": {
    "category": "Year",
    "value": "2038",
    "title": "First Year",
    "description": "",
    "orderby": "4"
  },
  "3": {
    "category": "Year",
    "value": "2016",
    "title": "Base Year",
    "description": "",
    "orderby": "1"
  },
  "4": {
    "category": "Year",
    "value": "2018",
    "title": "Previous Year",
    "description": "",
    "orderby": "2"
  }
}

Output required:

{
  "3": {
    "category": "Year",
    "value": "2016",
    "title": "Base Year",
    "description": "",
    "orderby": "1"
  },
  "4": {
    "category": "Year",
    "value": "2018",
    "title": "Previous Year",
    "description": "",
    "orderby": "2"
  },
  "1": {
    "category": "Year",
    "value": "2028",
    "title": "Current Year",
    "description": "",
    "orderby": "3"
  },
  "2": {
    "category": "Year",
    "value": "2038",
    "title": "First Year",
    "description": "",
    "orderby": "4"
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can sort this array by using javascript sort function.

const data = {
  "1": {
    "category": "Year",
    "value": "2028",
    "title": "Current Year",
    "description": "",
    "orderby": "3"
  },
  "2": {
    "category": "Year",
    "value": "2038",
    "title": "First Year",
    "description": "",
    "orderby": "4"
  },
  "3": {
    "category": "Year",
    "value": "2016",
    "title": "Base Year",
    "description": "",
    "orderby": "1"
  },
  "4": {
    "category": "Year",
    "value": "2018",
    "title": "Previous Year",
    "description": "",
    "orderby": "2"
  }
}
Object.keys(data).sort((a, b) => data[a].orderby - data[b].orderby).map(f => data[f])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!