Link to home
Start Free TrialLog in
Avatar of Rajar Ahmed
Rajar AhmedFlag for India

asked on

sort json string

how to sort the json string accordingly for the below example ?
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
    function srtodr()
        {
            var testString = "{ 'a':[ { 'Name':'Atest2','Age':'30','Date':'02/08/1988'}, { 'Name':'Zambort','Age':'25','Date':'30/10/1975'}, { 'Name':'Setest','Age':'41','Date':'24/12/1982'} ]} ";
            var oJSON = eval("(" + testString + ")");
            for (var i = 0; i < oJSON.a.length; i++)
            {
                conc.innerHTML = conc.innerHTML + "Name = " + oJSON.a[i].Name;
                conc.innerHTML = conc.innerHTML + " Age = " + oJSON.a[i].Age;
                conc.innerHTML = conc.innerHTML + " Date = " + oJSON.a[i].Date;
                conc.innerHTML = conc.innerHTML + "<br/>";
            }             
        }
    
    </script>
</head>
<body onload="srtodr()">
    <form id="form1" runat="server">
    <div id="conc"></div>
    <div>
      <a href="javascript:void(0)" onclick="srtodr()"> Sort Name </a> ||
       <a href="javascript:void(0)" onclick="srtodr()">Sort Age </a> ||
       <a href="javascript:void(0)" onclick="srtodr()">Sort Date </a> 
    </div>
    </form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rajar Ahmed

ASKER

almost , but its not changing to descending if d order is asc and vice versa .
That is an Example and the rest is your job.

Try to read the example and you will get the solution on your own which is much more worth then cut'n paste  my code.
Here a version from 2005: http:Q_21425003.html
thanks for ur wonderfull code.
i can play  with some filters using this
oJSON.a.reverse();
Hope it will work ..
Meeran03
Can you open a new Question for the sorting direction?