Home > Blockchain >  javascript - sort nested json object
javascript - sort nested json object

Time:05-30

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

CodePudding user response:

As pointed out in the comments by Robby Cornelissen, you can't control the ordering of keys in an object, so the only way to generate the JSON you want is to do it manually. One way to do this is by sorting the object entries and then appending them one by one to a JSON string:

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

json = '{\n'  
Object.entries(data)
  .sort((a, b) => a[1].orderby - b[1].orderby)
  .map(([k, v]) => `"${k}" : ${JSON.stringify(v, null, 2)}`)
  .join(',\n')  
'\n}'

console.log(json)
.as-console-wrapper { max-height : 100% !important }

Note I've added whitespace to make it obvious the output is what you want, that could be removed with no effect on the result.

CodePudding user response:

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])
  • Related