javascript - How to sort json object in jquery -


i have json object like:

{     "resp" : [         {             "name" : "s"         },          {             "name" : "c"         },          {             "name" : "f"         }     ] } 

here, want sort object name values in alphabetical order.
so, should become object:

{     "resp" : [         {             "name" : "c"         },          {             "name" : "f"         },         {             "name" : "s"         }     ] } 

how how can implement it?

you should sort array inside javascript object prior json serialization. serialized javascript object nothing more string , therefore shouldn't sorted. take @ this:

var obj = {     rest: [         { name: "s" },         { name: "c" },         { name: "f" }     ] };  function compare(a, b) {     if (a.name< b.name)         return -1;     if (a.name > b.name)         return 1;     return 0; }  obj.rest.sort(compare);  json.stringify(obj) 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -