Solved

sort json string

Posted on 2011-09-05
7
470 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Rajar Ahmed
  • 4
  • 3
7 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 36484907
Here you go:
<!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>Zvonko &#42;</title>
    <script>
    function srtodr(sFunc)
        {
            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 + ")");
			conc.innerHTML = "";
			oJSON.a.sort(sFunc);
            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/>";
            }             
        }
	function sortName(a,b){
	  if(a.Name>b.Name) return 1;
	  if(a.Name<b.Name) return -1;
	  return 0;
	}
	function sortAge(a,b){
	  return (a.Age*1)-(b.Age*1)
	}
	function sortDate(a,b){
	  var aDate = a.Date.split('/');
	  var bDate = b.Date.split('/');
	  aDate = new Date(aDate[2],aDate[1],aDate[0]);
	  bDate = new Date(bDate[2],bDate[1],bDate[0]);
	  return aDate.getTime()-bDate.getTime();
	}
	
    
    </script>
</head>
<body onload="srtodr(sortName)">
    <form id="form1" runat="server">
    <div id="conc"></div>
    <div>
      <a href="javascript:void(0)" onclick="srtodr(sortName)"> Sort Name </a> ||
       <a href="javascript:void(0)" onclick="srtodr(sortAge)">Sort Age </a> ||
       <a href="javascript:void(0)" onclick="srtodr(sortDate)">Sort Date </a> 
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 36484964
almost , but its not changing to descending if d order is asc and vice versa .
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36485059
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 63

Expert Comment

by:Zvonko
ID: 36485091
Here a version from 2005: http:Q_21425003.html
0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
ID: 36485098
thanks for ur wonderfull code.
i can play  with some filters using this
oJSON.a.reverse();
Hope it will work ..
Meeran03
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36485145
Can you open a new Question for the sorting direction?
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 36489597
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now