<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: reev0049</title>
    <description>The latest articles on DEV Community by reev0049 (@reev0049).</description>
    <link>https://dev.to/reev0049</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F728624%2Fd9b5e6fa-cd5b-4c7c-9910-31ac70080c44.png</url>
      <title>DEV Community: reev0049</title>
      <link>https://dev.to/reev0049</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reev0049"/>
    <language>en</language>
    <item>
      <title>JavaScript tutorial - Explaining Array.prototype.sort()</title>
      <dc:creator>reev0049</dc:creator>
      <pubDate>Mon, 18 Oct 2021 02:08:11 +0000</pubDate>
      <link>https://dev.to/reev0049/javascript-tutorial-explaining-arrayprototypesort-2m72</link>
      <guid>https://dev.to/reev0049/javascript-tutorial-explaining-arrayprototypesort-2m72</guid>
      <description>&lt;p&gt;So what is Array.prototype.sort()? As the name suggests, we are using an array and we'll be sorting it. There are also other neat functionalies like converting elements into strings. But for this example, we'll be taking a look on how we can rank our array from the greatest to least.&lt;/p&gt;

&lt;p&gt;Let's start with an array. Let's use a simple number integers. For this example we'll use an array of numbers that we want to sort.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Generating numbers...&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let nums = [7, 41, 48, 9, 11, 12]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay we have our arrary. The next step is to define a variable. Let's name it sortedNums.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sortedNums
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step would be adding a &lt;code&gt;spread operator.&lt;/code&gt; This neat syntax will allow us to add a copy so that we are able to compare them when we're done.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A spread operator in this example, creates a new array, then extracts all the elements we need from the original array, then plugs it in inside the new one. Essentially cloning the array.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sortedNums = [...nums]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we'll have to add a &lt;code&gt;callback function&lt;/code&gt;, so it understands what the logic we want going on. We also need to define two parameters as it requires. So lets name the parameters as A and B.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sortedNums.sort((a, b =&amp;gt; {})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the empty brackets, it'll spit out three posible things. &lt;code&gt;A positive number, negative number, and a zero.&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Positive and neagive numbers will each sort very differely. Pos numbers may sort from greatest to least and vice versa. Howevever, if it spits out zero, then no changes were made!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P2YuELq_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuh3kjmd14hqz35jn9qr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P2YuELq_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuh3kjmd14hqz35jn9qr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now onto how it computes the results. If you take a look at the crude diagram, &lt;code&gt;theres a recurring pattern.&lt;/code&gt; It's shifting those pairs for comparison one index position at a time on each iteration of the loop.&lt;/p&gt;

&lt;p&gt;Don't forget to add &lt;code&gt;console.log(sortedNums)&lt;/code&gt; at the end to get results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict'

let nums = [7, 41, 48, 9, 11, 12]
let sortedNums = [...nums]
sortedNums.sort((a, b =&amp;gt; b - a)
console.log(sortedNums)
console.log(nums)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node sort.js
[ 48, 41, 12, 11, 9, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The end result should look something like this.&lt;/p&gt;


&lt;h2&gt; Conclusion &lt;h2&gt;

&lt;/h2&gt;
&lt;/h2&gt;
&lt;p&gt;The logic in this tutorial is very rudimentary and it only serves to introduce sort() with simple numbers integers. You can take it a step further by adding more complex logic.&lt;/p&gt;

&lt;p&gt;For a more complex logic, we may say... If A minus B, is greater than zero then return one. Et cetera.&lt;/p&gt;

&lt;p&gt;Numbers can also be substituted for months, fruits, or even days of the week. Depending on what suits your needs.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
