<?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: Jamil Siam</title>
    <description>The latest articles on DEV Community by Jamil Siam (@siamcr7).</description>
    <link>https://dev.to/siamcr7</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%2F116899%2F473e235b-0198-475b-a437-830d1a611ef3.jpeg</url>
      <title>DEV Community: Jamil Siam</title>
      <link>https://dev.to/siamcr7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siamcr7"/>
    <language>en</language>
    <item>
      <title>Default Javascript Array Sorting</title>
      <dc:creator>Jamil Siam</dc:creator>
      <pubDate>Sun, 17 Jan 2021 16:04:44 +0000</pubDate>
      <link>https://dev.to/siamcr7/default-javascript-array-sorting-ifl</link>
      <guid>https://dev.to/siamcr7/default-javascript-array-sorting-ifl</guid>
      <description>&lt;p&gt;I was recently solving a simple &lt;a href="https://codeforces.com/contest/1473/problem/A"&gt;programming problem&lt;/a&gt; with TS, where I had to sort an array of integers in ascending order as part of the solution. So, naturally, I resorted to the &lt;code&gt;array.sort()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;To my surprise, I was getting a wrong answer verdict even though I was certain that my solution was correct. It turned out that TS (or rather JS) array default sorting behaviour has a quite confusing caveat.&lt;/p&gt;

&lt;p&gt;To get a better understanding of what I am talking about, let's see the following code:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[10, 2, 1].sort()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Snippet 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['10', '2', '1'].sort()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In C++/C#/Java/C, the snippet 1 would return &lt;code&gt;[1, 2, 10]&lt;/code&gt; and snippet 2 would return &lt;code&gt;['1', '10', '2']&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, in JS world, both of the snippets would return the same order (&lt;code&gt;[1, 10, 2]&lt;/code&gt; and &lt;code&gt;['1', '10', '2']&lt;/code&gt; respectively) because in JS the default behaviour is to convert the values into strings and then run sorting (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#content:~:text=built%20upon%20converting%20the%20elements%20into,sequences%20of%20UTF%2D16%20code%20units%20values"&gt;details&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;For &lt;strong&gt;Snippet 1&lt;/strong&gt; to work correctly in JS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[10, 2, 1].sort((a, b) =&amp;gt; a - b)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for today!&lt;/p&gt;

&lt;p&gt;PS. This is my first post in dev.to or in any platform&lt;br&gt;
PSS. If you are interested to know about solving programming problems in TS feel free to check out my &lt;a href="https://github.com/siamcr7/ts-node-problem-solving"&gt;repo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>sorting</category>
    </item>
    <item>
      <title>An awesome cheat sheet</title>
      <dc:creator>Jamil Siam</dc:creator>
      <pubDate>Fri, 23 Nov 2018 03:38:48 +0000</pubDate>
      <link>https://dev.to/siamcr7/an-awesome-cheat-sheet-hld</link>
      <guid>https://dev.to/siamcr7/an-awesome-cheat-sheet-hld</guid>
      <description>

&lt;p&gt;Here is an awesome cheat sheet from the good people at StackOverflow&lt;/p&gt;

&lt;p&gt;Link: &lt;a href="https://books.goalkicker.com"&gt;https://books.goalkicker.com&lt;/a&gt;&lt;/p&gt;


</description>
      <category>cheatsheet</category>
    </item>
  </channel>
</rss>
