<?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>JavaScript Blog &#8212; onthecode</title>
	<atom:link href="https://onthecode.co.uk/blog/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>https://onthecode.co.uk/blog/category/javascript</link>
	<description>onthecode blog</description>
	<lastBuildDate>Mon, 21 Aug 2023 21:37:48 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://onthecode.co.uk/wp-content/uploads/2019/02/onthecode-icon-1-100x100.png</url>
	<title>JavaScript Blog &#8212; onthecode</title>
	<link>https://onthecode.co.uk/blog/category/javascript</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Sort Array of Objects by Date in JavaScript</title>
		<link>https://onthecode.co.uk/blog/how-to-sort-array-of-objects-by-date-in-javascript</link>
					<comments>https://onthecode.co.uk/blog/how-to-sort-array-of-objects-by-date-in-javascript#comments</comments>
		
		<dc:creator><![CDATA[Umut Esen]]></dc:creator>
		<pubDate>Fri, 13 Sep 2019 16:35:32 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript sort]]></category>
		<guid isPermaLink="false">https://onthecode.co.uk/?p=2644</guid>

					<description><![CDATA[<p>I recently had to figure out how to use JavaScript sort to order objects by date property. JavaScript sort method is available on array objects in JavaScript. It mutates the array, which means it modifies the elements of the array while sorting. The sort method has excellent support across all browsers. This makes it a great candidate to use for sorting an array of objects. Let&#8217;s look at how to use the built-in Array.sort method to sort an array of objects by date in descending or ascending order. JavaScript sort array syntax As you can see above, the sort method takes in a single optional parameter called compareFunction. If you don&#8217;t supply a function, it converts each element to a string for comparison using code point. Notice anything weird? JavaScript sort converts each element to string and applies alphabetical sorting. Implementing a custom compare function Since sorting dates with string representations is not a great idea, we will implement the compareFunction for sorting arrays by date property. The syntax is below: The compare function above must return a number. Depending on the number returned, JavaScript sort method will adjust the position of elements. It is worth noting the function above ignores all undefined elements of the array. By default, it places all undefined elements at the end after sorting the array. Sort by date in ascending order Having explored the sort method syntax, we apply sorting in descending order. Sort by date in descending order We can reverse the sort order simply by inverting the return value of the function. Summary We have just implemented JavaScript sort to order an array of objects by date property. We learned how to implement custom comparison using the sort method. If you have any questions, feel free to drop a line below.</p>
<p>The post <a href="https://onthecode.co.uk/blog/how-to-sort-array-of-objects-by-date-in-javascript">How to Sort Array of Objects by Date in JavaScript</a> appeared first on <a href="https://onthecode.co.uk">onthecode</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>I recently had to figure out how to use <strong><a rel="noreferrer noopener" aria-label="JavaScript (opens in a new tab)" href="https://onthecode.co.uk/blog/category/javascript/" target="_blank">JavaScript</a> sort</strong> to order objects by date property. JavaScript sort method is available on array objects in JavaScript. It mutates the array, which means it modifies the elements of the array while sorting. The sort method has excellent support across all browsers. This makes it a great candidate to use for sorting an array of objects. </p>



<p>Let&#8217;s look at how to use the built-in <a rel="noreferrer noopener" href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" target="_blank">Array.sort</a> method to sort an array of objects by date in <strong>descending</strong> <strong>or ascending</strong> order.</p>






<h2 class="wp-block-heading">JavaScript sort array syntax</h2>


<pre class="wp-block-code"><span><code class="hljs language-javascript">myArray.sort(&#091;compareFunction]);</code></span></pre>


<p>As you can see above, the sort method takes in a single <strong>optional</strong> parameter called <code>compareFunction</code>. If you don&#8217;t supply a function, it converts each element to a string for comparison using <a href="https://en.wikipedia.org/wiki/Code_point" target="_blank" rel="noreferrer noopener">code point</a>. </p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-keyword">var</span> foo = &#091;<span class="hljs-number">9</span>, <span class="hljs-number">30</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'what'</span>, <span class="hljs-string">'the'</span>];
foo.sort(); <span class="hljs-comment">// returns &#091;30, 5, 9, 'the', 'what']</span></code></span></pre>


<p>Notice anything weird? JavaScript sort converts each element to string and applies alphabetical sorting.</p>



<h2 class="wp-block-heading">Implementing a custom compare function</h2>



<p>Since sorting dates with string representations is not a great idea, we will implement the <code>compareFunction</code> for sorting arrays by date property. </p>



<p>The syntax is below:</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">compare</span>(<span class="hljs-params">firstElement, secondElement</span>) </span>{
  <span class="hljs-comment">// must return a number</span>
}</code></span></pre>


<p>The <code>compare</code> function above must return a number. Depending on the number returned, JavaScript sort method will adjust the position of elements.</p>



<ol class="wp-block-list">
<li>If&nbsp;the result is less than 0, <strong>firstElement</strong> comes first.</li>



<li>If&nbsp;the result is 0, it leaves&nbsp;<strong>firstElement</strong>&nbsp;and&nbsp;<strong>secondElement</strong>&nbsp;unchanged with respect to each other, but sorted with respect to all other elements.</li>



<li>If&nbsp;the result is greater than 0,&nbsp;<strong>secondElement</strong> comes first.</li>
</ol>



<p>It is worth noting the function above ignores all undefined elements of the array. </p>



<p>By default, it places all undefined elements at the end after sorting the array.</p>



<h3 class="wp-block-heading">Sort by date in ascending order</h3>



<p>Having explored the sort method syntax, we apply sorting in descending order.</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-keyword">var</span> friends = &#091;
   { <span class="hljs-attr">name</span>: <span class="hljs-string">'Middle-aged John'</span>, <span class="hljs-attr">dateOfBirth</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-number">1995</span>, <span class="hljs-number">5</span>, <span class="hljs-number">19</span>)},
   { <span class="hljs-attr">name</span>: <span class="hljs-string">'Young John'</span>, <span class="hljs-attr">dateOfBirth</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-number">2000</span>, <span class="hljs-number">1</span>, <span class="hljs-number">22</span>)},
   { <span class="hljs-attr">name</span>: <span class="hljs-string">'Old John'</span>, <span class="hljs-attr">dateOfBirth</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-number">1990</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>)},
];

friends.sort(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">if</span> (a.dateOfBirth &gt; b.dateOfBirth) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
  <span class="hljs-keyword">if</span> (a.dateOfBirth &lt; b.dateOfBirth) <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
});

<span class="hljs-built_in">console</span>.log(friends);
<span class="hljs-comment">// Prints in order: Old John, Middle-aged John, Young John</span></code></span></pre>


<h2 class="wp-block-heading">Sort by date in descending order</h2>



<p>We can reverse the sort order simply by inverting the return value of the function.</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-keyword">var</span> friends = &#091;
   { <span class="hljs-attr">name</span>: <span class="hljs-string">'Middle-aged John'</span>, <span class="hljs-attr">dateOfBirth</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-number">1995</span>, <span class="hljs-number">5</span>, <span class="hljs-number">19</span>)},
   { <span class="hljs-attr">name</span>: <span class="hljs-string">'Young John'</span>, <span class="hljs-attr">dateOfBirth</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-number">2000</span>, <span class="hljs-number">1</span>, <span class="hljs-number">22</span>)},
   { <span class="hljs-attr">name</span>: <span class="hljs-string">'Old John'</span>, <span class="hljs-attr">dateOfBirth</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-number">1990</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>)},
];

friends.sort(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">if</span> (a.dateOfBirth &gt; b.dateOfBirth) <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
  <span class="hljs-keyword">if</span> (a.dateOfBirth &lt; b.dateOfBirth) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
});

<span class="hljs-built_in">console</span>.log(friends);
<span class="hljs-comment">// Prints in order: Young John, Middle-aged John, Old John</span></code></span></pre>


<h2 class="wp-block-heading">Summary</h2>



<p>We have just implemented JavaScript sort to order an array of objects by date property. We learned how to implement custom comparison using the sort method. </p>



<p>If you have any questions, feel free to drop a line below. </p>
<p>The post <a href="https://onthecode.co.uk/blog/how-to-sort-array-of-objects-by-date-in-javascript">How to Sort Array of Objects by Date in JavaScript</a> appeared first on <a href="https://onthecode.co.uk">onthecode</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://onthecode.co.uk/blog/how-to-sort-array-of-objects-by-date-in-javascript/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
