<?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: Daniel Dennis</title>
    <description>The latest articles on DEV Community by Daniel Dennis (@katungi).</description>
    <link>https://dev.to/katungi</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%2F175598%2Fc25992b7-4aef-4f0d-95da-9f7cafafd621.png</url>
      <title>DEV Community: Daniel Dennis</title>
      <link>https://dev.to/katungi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/katungi"/>
    <language>en</language>
    <item>
      <title>JS, method behind the madness.</title>
      <dc:creator>Daniel Dennis</dc:creator>
      <pubDate>Tue, 08 Sep 2020 17:21:22 +0000</pubDate>
      <link>https://dev.to/katungi/js-method-behind-the-madness-10ff</link>
      <guid>https://dev.to/katungi/js-method-behind-the-madness-10ff</guid>
      <description>&lt;h1&gt;
  
  
  Hi There, Welcome to my first Post ☄
&lt;/h1&gt;

&lt;p&gt;It all started with this meme&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9IxsMeI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7t9f5xx66trkqzen9hgv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9IxsMeI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7t9f5xx66trkqzen9hgv.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A friend of mine called me out as a now lover of JS. To be honest, i was mad(mostly because I didn't have a come back) so i decided to try it myself, only to prove him right. If you are not keen enough, JS may get away with it. Allow me to demonstrate the problem.&lt;/p&gt;

&lt;p&gt;Given an array of numbers [6,-2,2,-7], and sort them in Ruby and in JS using the Sort method &lt;code&gt;.sort()&lt;/code&gt;.&lt;br&gt;
In JS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// The result is: [ -2, -7, 2, 6 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In Ruby:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
 &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

 &lt;span class="c1"&gt;# The Result is:  [-7, -2, 2, 6]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By now, I hope you see it now. If you are a beginner, Given a couple of numbers and telling the computer to sort the numbers using different programming languages, one language gives them back arranged correctly (Ruby), and the other gives a not so correct answer(JS).&lt;/p&gt;

&lt;p&gt;The reason why this happens is because, Javascript is a weird language. Not weird, Just different. Javascript's sort method uses an &lt;a href="https://www.wikiwand.com/en/In-place_algorithm"&gt;In-Place&lt;/a&gt; algorithm to sort through the array elements.&lt;/p&gt;

&lt;p&gt;This basically means, It converts the elements in an array into a sequence of Strings, then compares the sequences of UTF-16 code units values. In simple terms, makes the numbers become individual strings of letters and arranges them accordingly.&lt;br&gt;
Now, the sort method isn't broken, to see this underlying in-place algorithm work, lets sort some Strings.&lt;/p&gt;

&lt;p&gt;Given an array of Names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Daniel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fizz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buzz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cynthia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// The result is: [ 'Bob', 'Buzz', 'Cynthia', 'Daniel', 'Fizz' ]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Tada ☄☄ It works because we are sorting strings. The default behavior of the sort method will treat everything like a String.&lt;/p&gt;

&lt;p&gt;Worry not, You can override this behavior by use of a &lt;code&gt;parameter function&lt;/code&gt;.&lt;br&gt;
Fasten your seatbelt, it may get tricky. The function will take 2 parameters itself say &lt;code&gt;(a,b)&lt;/code&gt;, To learn more on the function and it's capabilities, read &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort"&gt;this&lt;/a&gt;. The function basically just checks whether a or b is smaller, bigger or equal to the other and arranges them accordingly,regardless of the data type.&lt;/p&gt;

&lt;p&gt;To tell the sort method to treat the elements in the array as numbers, &lt;code&gt;just subtract the parameters (a,b) of the parameter function of the sort method&lt;/code&gt;. This will arrange the elements in ascending order irrespective of the data type.&lt;br&gt;
 Confusing, let me demonstrate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//given a and b are parameters of Compare function which we pass to the sort method.&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// The result will be correct, you can check 😉&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yikes, that looks weird, lets redo it with a &lt;code&gt;fat arrow function&lt;/code&gt; like sane people.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// The result will be correct, you can check again 😉&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Much better now courtesy of &lt;a href="https://dev.to/pentacular"&gt;pentacular&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And there you have it, a well sorted array.&lt;/p&gt;

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

&lt;p&gt;Moral of the story is, If you have a farm, I highly recommend you turn to farming instead, because talking to computers using JS will make you cry at one point. Ha!, just kidding, Javascript has alot of weird stuff here and there, but when you love something, you love the flaws too.&lt;br&gt;
Just a tip, if you don't like all the weird parts of JS, try &lt;a href="http://typescriptlang.org/"&gt;Typescript&lt;/a&gt; by Microsoft which offers Types and other cool features on top of JS.&lt;/p&gt;

&lt;p&gt;Till Next rant,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Daniel Katungi Dennis&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
