0

I am getting below response on hitting DoctorData.api and want to sort them all with their 'ID'. Can someone show me how to sort the Output JSON data and display it same format. Kindly Excuse my coding skills, this is my second test case. I am new to JS.

    var doctorIDgeturl = geturl.geturls.getapiUrl; //'getapiUrl' is Doctor Get API
    var res = await api.getRequest(doctorIDgeturl);
    logger.logger().info('GET_data = ', JSON.stringify(res.data, null, 2));
    var rescount = Object.keys(res.data.data.doctorList); //doctorList is the API response object for above GET API
  
    console.log("This is Sorted Id: ");
    const sortedResponse = sort(res.data, r => r.doctorListModels.associateId, ['asc']) //using ascending order to sort
    console.log(sortedResponse);

Current output:

{
    "message": "Record Found",
    "data": {
        "DoctorsList": [
        {
            "id": "10",
            "name": "William",
            "launch_date": "2018-01-24T00:00:00.000-05:00"
        },
        {
            "id": "2",
            "name": "Snow",
            "launch_date": "2017-08-14T00:00:00.000-05:00"
        },
        {
            "id": "33",
            "name": "Thomas",
            "launch_date": "2018-11-29T00:00:00.000-05:00"
        },
        {
            "id": "3",
            "name": "Ismail",
            "launch_date": "2018-11-29T00:00:00.000-05:00"
        },
        {
            "id": "5",
            "name": "Jackson",
            "launch_date": "2018-04-10T00:00:00.000-05:00"
        }

Expected output after sorting:

{
    "message": "Record Found",
    "data": {
        "DoctorsList": [

   {
        "id": "2",
        "name": "Snow",
        "launch_date": "2017-08-14T00:00:00.000-05:00"
    },
    {
        "id": "3",
        "name": "Ismail",
        "launch_date": "2018-11-29T00:00:00.000-05:00"
    },
    {
        "id": "5",
        "name": "Jackson",
        "launch_date": "2018-04-10T00:00:00.000-05:00"
    },
    {
        "id": "10",
        "name": "William",
        "launch_date": "2018-01-24T00:00:00.000-05:00"
    },
    {
        "id": "33",
        "name": "Thomas",
        "launch_date": "2018-11-29T00:00:00.000-05:00"
    }
James Z
  • 12,209
  • 10
  • 24
  • 44
Mad Einsy
  • 39
  • 1
  • 8
  • 1
    Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Christiaan Westerbeek May 22 '20 at 21:10

3 Answers3

0

parseInt(r.doctorListModels.associateId) or +r.doctorListModels.associateId

it seems like it sorts the id as string not number

0

Sorry I can't comment because of low reputation, but here is solution.

const obj = {
  "message": "Record Found",
  "data": {
    "DoctorsList": [{
      "id": "10",
      "name": "William",
      "launch_date": "2018-01-24T00:00:00.000-05:00"
    },
    {
      "id": "2",
      "name": "Snow",
      "launch_date": "2017-08-14T00:00:00.000-05:00"
    },
    {
      "id": "33",
      "name": "Thomas",
      "launch_date": "2018-11-29T00:00:00.000-05:00"
    },
    {
      "id": "3",
      "name": "Ismail",
      "launch_date": "2018-11-29T00:00:00.000-05:00"
    },
    {
      "id": "5",
      "name": "Jackson",
      "launch_date": "2018-04-10T00:00:00.000-05:00"
    }]
  }
}


const sortedResponse = obj.data.DoctorsList.sort(function(a, b) { return parseInt(a.id) - parseInt(b.id) });
console.log(sortedResponse)
Jason
  • 51
  • 5
-1

const obj = {
    "message": "Record Found",
    "data": {
        "DoctorsList": [{
    "id": "10",
    "name": "William",
    "launch_date": "2018-01-24T00:00:00.000-05:00"
  },
  {
    "id": "2",
    "name": "Snow",
    "launch_date": "2017-08-14T00:00:00.000-05:00"
  },
  {
    "id": "33",
    "name": "Thomas",
    "launch_date": "2018-11-29T00:00:00.000-05:00"
  },
  {
    "id": "3",
    "name": "Ismail",
    "launch_date": "2018-11-29T00:00:00.000-05:00"
  },
  {
    "id": "5",
    "name": "Jackson",
    "launch_date": "2018-04-10T00:00:00.000-05:00"
  }
]
}}


obj.data.DoctorsList = obj.data.DoctorsList.sort((a, b) => parseInt(a.id) > parseInt(b.id));
console.log(obj)
max
  • 310
  • 1
  • 6
  • Did you review your output before answering? It produces the exact **incorrect** output form the question.. – Brian Thompson May 22 '20 at 21:10
  • No it doesn't. Check again. – Brian Thompson May 22 '20 at 21:19
  • This is my output: https://pastebin.com/7qkzeDE3 So it is ordered. I don't see the error here – max May 22 '20 at 21:40
  • appologies if i miss the code sir, as my current code is incomplete im struggling to make it right during this Stressed Situation, we are helping doctors to get the most of their details so none get over flow with work, to achieve it we asked from client to make it automated and get the ID's. So i thot of asking here to which sort function i can use to produce the json output. – Mad Einsy May 22 '20 at 22:27
  • @zirmax But that isn't the output. Its in a snippet, if you run the snippet, that is not what you see logged. – Brian Thompson May 22 '20 at 22:29