<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Json Array &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/json-array/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Fri, 17 Jul 2015 12:26:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Json Array &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Sort a JSON Array Programmatically by a Property</title>
		<link>https://www.sibeeshpassion.com/sort-a-json-array-programmatically-by-a-property/</link>
					<comments>https://www.sibeeshpassion.com/sort-a-json-array-programmatically-by-a-property/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 10 Jun 2015 00:00:47 +0000</pubDate>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Json]]></category>
		<category><![CDATA[Json Array]]></category>
		<category><![CDATA[Sort]]></category>
		<category><![CDATA[Sort array by property]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=2141</guid>

					<description><![CDATA[Introduction Hi all, I hope you all are fine. Today we will learn how to sort a JSON object by its property. We will be using normal jQuery features to do this. I hope you will like it. Background Yesterday I got a situation where I needed to sort my JSON object that I am creating using server data. So I though of sharing that with you all. Using the code First of all we will add a jQuery reference as in the following: [js] &#60;script src=“jquery-2.1.4.min.js”&#62;&#60;/script&#62; [/js] Now we need some, data right? Please have a look at the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>Hi all, I hope you all are fine. Today we will learn how to sort a JSON object by its property. We will be using normal jQuery features to do this. I hope you will like it.</p>
<p><strong>Background</strong></p>
<p>Yesterday I got a situation where I needed to sort my JSON object that I am creating using server data. So I though of sharing that with you all.</p>
<p><strong>Using the code</strong></p>
<p>First of all we will add a jQuery reference as in the following:<br />
[js]<br />
&lt;script src=“jquery-2.1.4.min.js”&gt;&lt;/script&gt;<br />
[/js]</p>
<p>Now we need some, data right? Please have a look at the following JSON data.</p>
<p>[js]<br />
var data = ‘[{“name”:2014,”data”:[{“x”:”1″,”y”:222808746.81}]},{“name”:2013,”data”:[{“x”:”2″,”y”:289647045.18}]},{“name”:2014,”data”:[{“x”:”2″,”y”:285136890.07}]},{“name”:2013,”data”:[{“x”:”3″,”y”:370853178.74}]},{“name”:2014,”data”:[{“x”:”3″,”y”:403272964.28}]},{“name”:2012,”data”:[{“x”:”4″,”y”:217294031.36}]},{“name”:2013,”data”:[{“x”:”4″,”y”:224715039.94}]},{“name”:2014,”data”:[{“x”:”4″,”y”:249034460.23}]},{“name”:2012,”data”:[{“x”:”5″,”y”:215978054.15}]},{“name”:2013,”data”:[{“x”:”5″,”y”:241211810.92}]}]’;<br />
[/js]</p>
<p>Next we need some UI elements. Am I right?<br />
[html]<br />
&lt;div id=“unsorted”&gt;&lt;/div&gt;<br />
    &lt;div id=“sorted”&gt;&lt;/div&gt;<br />
    &lt;button id=“makemesort”&gt;Make Me Sort&lt;/button&gt;<br />
[/html]</p>
<p>What next?  We need to show this data to our UI, right? For that I am calling a function in our document ready function.<br />
[js]<br />
var jsonObject;<br />
        $(function () {<br />
            $(‘#sorted’).hide();<br />
            loadUnsorted();<br />
            $(‘#makemesort’).click(function () {<br />
                loadSorted();<br />
                $(‘#makemesort’).hide();<br />
                $(‘#sorted’).show();<br />
            });<br />
        });<br />
[/js]</p>
<p>The following is the function definition for the loadUnsorted() function.<br />
[js]<br />
function loadUnsorted() {<br />
           jsonObject = $.parseJSON(data);<br />
           var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
           for (i = 0; i &lt; jsonObject.length; i++) {<br />
               html += ‘&lt;tr&gt;&lt;td&gt;’ + jsonObject[i].name + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
           }<br />
           html += ‘&lt;/table&gt;’;<br />
           $(‘#unsorted’).append(html);<br />
       }<br />
[/js]</p>
<p>What we are doing here is parsing our data and appending the data to our UI element using a for loop.</p>
<p>Now if you run it, you will get output as follows.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/sort-a-json-array-by-a-property-programatically/Images/srtjsn1.PNG" alt="" /></p>
<p>Now we have successfully formatted our data and shown it to our UI.  Cool, right?<br />
So shall we go and sort our data? I guess you said “Yes”. Awesome.<br />
Now we will create a click event for our “Make me sort” button as in the following:<br />
[js]<br />
$(‘#makemesort’).click(function () {<br />
                loadSorted();<br />
                $(‘#makemesort’).hide();<br />
                $(‘#sorted’).show();<br />
            });<br />
[/js]</p>
<p>So here is the definition for the loadSorted() function.<br />
[js]<br />
function loadSorted() {<br />
           jsonObject.sort(SortMe);<br />
           var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
           for (i = 0; i &lt; jsonObject.length; i++) {<br />
               html += ‘&lt;tr&gt;&lt;td&gt;’ + jsonObject[i].name + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
           }<br />
           html += ‘&lt;/table&gt;’;<br />
           $(‘#sorted’).append(html);<br />
       }<br />
[/js]</p>
<p>Now I guess you could determine the difference of both the loadSorted() and loadUnsorted() functions. Yeah, you are right. I am calling a sort function “sortMe”  there.<br />
[js]<br />
function SortMe(a, b) {<br />
           if (b.name != null &amp;&amp; b.name != undefined &amp;&amp; a.name != null &amp;&amp; a.name != undefined) {<br />
               var First = a.name.toString().toLowerCase();<br />
               var Second = b.name.toString().toLowerCase();<br />
               return ((First &lt; Second) ? -1 : ((First &gt; Second) ? 1 : 0));<br />
           }<br />
       }<br />
[/js]</p>
<p>What our “sortMe” function does is, it will take two objects at a time and compare those by the property “name” of each object.</p>
<p>[js]<br />
(First &lt; Second) ? -1 : ((First &gt; Second) ? 1 : 0)<br />
[/js]</p>
<p>Now shall we look into the complete code? We have done everything folks.</p>
<p><strong>Complete Code</strong></p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Sort JSON Object by its property and show demo – Sibeesh Passion&lt;/title&gt;<br />
    &lt;script src=“jquery-2.1.4.min.js”&gt;&lt;/script&gt;<br />
    &lt;style&gt;<br />
        #unsorted {<br />
            border: 1px solid #999;<br />
            width:220px;<br />
            padding:10px;<br />
            float:left;<br />
        }<br />
        #sorted {<br />
            border: 1px solid #999;<br />
            width:220px;<br />
            padding:10px;<br />
            float:left;<br />
        }<br />
         td {<br />
            border: 1px solid #ccc;<br />
            padding: 5px;<br />
            text-align: center;<br />
        }<br />
    &lt;/style&gt;<br />
    &lt;script&gt;<br />
        var data = ‘[{“name”:2014,”data”:[{“x”:”1″,”y”:222808746.81}]},{“name”:2013,”data”:[{“x”:”2″,”y”:289647045.18}]},{“name”:2014,”data”:[{“x”:”2″,”y”:285136890.07}]},{“name”:2013,”data”:[{“x”:”3″,”y”:370853178.74}]},{“name”:2014,”data”:[{“x”:”3″,”y”:403272964.28}]},{“name”:2012,”data”:[{“x”:”4″,”y”:217294031.36}]},{“name”:2013,”data”:[{“x”:”4″,”y”:224715039.94}]},{“name”:2014,”data”:[{“x”:”4″,”y”:249034460.23}]},{“name”:2012,”data”:[{“x”:”5″,”y”:215978054.15}]},{“name”:2013,”data”:[{“x”:”5″,”y”:241211810.92}]}]’;<br />
        var jsonObject;<br />
        $(function () {<br />
            $(‘#sorted’).hide();<br />
            loadUnsorted();<br />
            $(‘#makemesort’).click(function () {<br />
                loadSorted();<br />
                $(‘#makemesort’).hide();<br />
                $(‘#sorted’).show();<br />
            });<br />
        });<br />
        function loadUnsorted() {<br />
            jsonObject = $.parseJSON(data);<br />
            var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
            for (i = 0; i &lt; jsonObject.length; i++) {<br />
                html += ‘&lt;tr&gt;&lt;td&gt;’ + jsonObject[i].name + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
            }<br />
            html += ‘&lt;/table&gt;’;<br />
            $(‘#unsorted’).append(html);<br />
        }<br />
        function loadSorted() {<br />
            jsonObject.sort(SortMe);<br />
            var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
            for (i = 0; i &lt; jsonObject.length; i++) {<br />
                html += ‘&lt;tr&gt;&lt;td&gt;’ + jsonObject[i].name + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
            }<br />
            html += ‘&lt;/table&gt;’;<br />
            $(‘#sorted’).append(html);<br />
        }<br />
        function SortMe(a, b) {<br />
            if (b.name != null &amp;&amp; b.name != undefined &amp;&amp; a.name != null &amp;&amp; a.name != undefined) {<br />
                var First = a.name.toString().toLowerCase();<br />
                var Second = b.name.toString().toLowerCase();<br />
                return ((First &lt; Second) ? -1 : ((First &gt; Second) ? 1 : 0));<br />
            }<br />
        }<br />
    &lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;div id=“unsorted”&gt;&lt;/div&gt;<br />
    &lt;div id=“sorted”&gt;&lt;/div&gt;<br />
    &lt;button id=“makemesort”&gt;Make Me Sort&lt;/button&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>So now it is time to see our output. I am eager to see it.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/sort-a-json-array-by-a-property-programatically/Images/srtjsn2.PNG" alt="" /></p>
<p>You can find we have sorted our object and shown it in a separate table. Cool.<br />
That is all for the day. I will return with another article soon.</p>
<p><strong>Conclusion</strong></p>
<p>I hope you liked this article. Please share me your valuable thoughts and comments. Your feedbacks are always welcome. Thanks in advance. Happy coding!.</p>
<p>Kindest Regards.<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/sort-a-json-array-programmatically-by-a-property/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
