locked
how to sort data which is in JSON format?? RRS feed

  • Question

  •  var dataArray1 = [
        { title: "Adams Carbaryl", text: " ", Id: "FB10009002" },
         { title: "Advanced Formula Instant Krazy Glue,", text: " ", Id: "GA10008002" },
        { title: "Alberto Culver Motions (Ethnic),", text: "", Id: "FB10009002" },
         { title: "Aleenes Laminate It,", text: "", Id: "ET10003002" },
    { title: "Air Wick Aerosols Papaya & Mango,", text: "", Id: "FB10009002" },
    { title: "18G Tight Head Plain Barrel,", text: " ", Id: "GH10009002" },
       { title: "Air Wick Aerosols Papaya & Mango,", text: "", Id: "TY10009002" },
    { title: "Alberto Culver Motions (Ethnic),", text: " ", Id: "TR10009002" },
    { title: "Aleenes Laminate It,", text: "", Id: "YU10009002" },
    { title: "Aleenes Leather and Suede,", text: " ", Id: "UY10009002" },
    { title: "18G Tight Head Plain Barrel,", text: "", Id: "ER10009002" },
    { title: "18G Tight Head Plain Barrel,", text: "", Id: "GH10009002" },
    { title: "Aleenes Laminate It,", text: "", Id: "YU10009002" },
    { title: "Aleenes Leather and Suede Glue,", text: " ", Id: "UY10009002" }


                ];

    How to sort above data by name?? thanks in advance

    Tuesday, April 9, 2013 5:13 AM

Answers

  • By name do you mean title?

    You need to use Array.sort with a sort function, i.e:

    sortedArray = dataArray1.sort(function(a,b) { 
      if (a.title > b.title) { 
        return 1 
      } else if (a.title < b.title) { 
        return -1 
      } else { 
        return 0 
      }
    });

    • Proposed as answer by Ealsur Tuesday, April 9, 2013 2:29 PM
    • Marked as answer by Yanping WangModerator Tuesday, April 16, 2013 6:06 AM
    Tuesday, April 9, 2013 7:56 AM