0

In the below json file i want to sort in descending order the content within docs field. There are two blocks . The data is dynamic meaning there can be multiple blocks-- it can be 5, it can be 10 such block within docs.

{
"Scoring":1.68466776,
"_text_":[
],
"id":"health_analytics.pdf",
"jac_Score":0.07370184254606366,
"matchScore":0.65,
"scaledTf_idf":0.0,
"searchTerm":[
"Machine learning",
"Deep learning",
"R",
"Python",
"Sas"
],
"sortingScore":59.78
}

and,

{
    "Scoring":2.029842275,
    "_text_":[
    ],
    "id":"customer_channel_analytics_bang.pdf",
    "jac_Score":0.09041591320072333,
    "matchScore":0.6875,
    "scaledTf_idf":0.0,
    "searchTerm":[
    "Machine learning",
    "Deep learning",
    "Sas",
    "R"
    ],
    "sortingScore":44.03
    },

I want to sort this using the Sortingscoring which is one of the fields within docs. The whole chunks(in this case there are two) within docs should be sorted.So after sorting the final json should be like below. I have referred to few of the stackoverflow links sort json object in javascript but not able to get it.

{
"statusCode":200,
"body":{
"docs":[
{
"Scoring":2.029842275,
"_text_":[
],
"id":"customer_channel_analytics_bang.pdf",
"jac_Score":0.09041591320072333,
"matchScore":0.6875,
"scaledTf_idf":0.0,
"searchTerm":[
"Machine learning",
"Deep learning",
"Sas",
"R"
],
"sortingScore":44.03
},
{
"Scoring":1.68466776,
"_text_":[
],
"id":"health_analytics.pdf",
"jac_Score":0.07370184254606366,
"matchScore":0.65,
"scaledTf_idf":0.0,
"searchTerm":[
"Machine learning",
"Deep learning",
"R",
"Python",
"Sas"
],
"sortingScore":59.78
}
]
}
}
Coder17
  • 767
  • 4
  • 11
  • 30

2 Answers2

0

Try this one

var a = {
      "Scoring":2.029842275,
      "_text_":[
      ],
      "id":"customer_channel_analytics_bang.pdf",
      "jac_Score":0.09041591320072333,
      "matchScore":0.6875,
      "scaledTf_idf":0.0,
      "searchTerm":[
      "Machine learning",
      "Deep learning",
      "Sas",
      "R"
      ],
      "sortingScore":44.03
  }
var b = {
      "Scoring":1.68466776,
      "_text_":[
      ],
      "id":"health_analytics.pdf",
      "jac_Score":0.07370184254606366,
      "matchScore":0.65,
      "scaledTf_idf":0.0,
      "searchTerm":[
      "Machine learning",
      "Deep learning",
      "R",
      "Python",
      "Sas"
      ],
      "sortingScore":59.78
}
var c = [ {a},{b}]
console.log(a.id)
console.log(b.id)
console.log(c.sort(function(a, b){
    return a - b;
}));
Amit Golhar
  • 799
  • 4
  • 8
  • 21
0
var json = `{
  "statusCode": 200,
  "body": {
    "docs": [
      {
        "Scoring": 2.029842275,
        "_text_": [],
        "id": "customer_channel_analytics_bang.pdf",
        "jac_Score": 0.09041591320072333,
        "matchScore": 0.6875,
        "scaledTf_idf": 0,
        "searchTerm": [
          "Machine learning",
          "Deep learning",
          "Sas",
          "R"
        ],
        "sortingScore": 64.03
      },
      {
        "Scoring": 1.68466776,
        "_text_": [],
        "id": "health_analytics.pdf",
        "jac_Score": 0.07370184254606366,
        "matchScore": 0.65,
        "scaledTf_idf": 0,
        "searchTerm": [
          "Machine learning",
          "Deep learning",
          "R",
          "Python",
          "Sas"
        ],
        "sortingScore": 59.78
      },
      {
        "Scoring": 3.029842275,
        "_text_": [],
        "id": "sex_anal_lyctics.pdf",
        "jac_Score": 0.09041591320072333,
        "matchScore": 0.6875,
        "scaledTf_idf": 0,
        "searchTerm": [
          "Machine learning",
          "Deep learning",
          "Sas",
          "R"
        ],
        "sortingScore": 14.03
      }
    ]
  }
}`;



var obj = JSON.parse(json);

for(var i = 0; i < obj.body.docs.length; i++){
  obj.body.docs.forEach(function(value,index){
    var counter = index;
    var temp;
    if(index < obj.body.docs.length-1){
      if(obj.body.docs[index].sortingScore > obj.body.docs[counter+1].sortingScore){
          temp = obj.body.docs[index];
          obj.body.docs[index] = obj.body.docs[counter+1];
          obj.body.docs[counter+1] = temp;
      }
    }
  });
}

console.log(obj.body.docs);

// hi there this is a sequential one i don't recommend if data is huge and just change comparison to > or < to suit your sort preference

View In Fiddle

Nyuu
  • 65
  • 1
  • 8
  • Hi Nyuu. I am using a tool which accepts javascript. When i put this code there i am getting syntax error. I believe the $.each which is jquery is not accepted by the tool. can you please change it to javascript. that would be very helpful. – Coder17 Feb 14 '19 at 07:03
  • actually (value,index) is javascript | jquery is (index,value) sorry bout that – Nyuu Feb 14 '19 at 07:26