Ajay Yadav
Ajay Yadav

@ATechAjay

22 Tweets 2 reads Jan 26, 2023
A complete thread on sort( ) method in JavaScript:๐Ÿงต
[2, 100, 70, 50].sort( )
โ‡ฉ
[100, 2, 50, 70]
Why?๐Ÿ˜ญA
๐Ÿ˜Š So, don't cry now in this thread we are going to see how the sort() method works behind the scenes.
๐ŸŽฏ My main focus is on how this "sort" method works behind the scenes.
๐Ÿง  In short, we are going to see multiple examples, so that you can easily understand.
let's go...๐Ÿ”ฅ
๐Ÿ’ก Overview of sort( )
โ The sort method allows us to sort the elements of the array.
โ It changes the position of the elements in the original array, in other words, the sort method mutates the original array.
โ By default, it sorts the array items in ascending order, which means the smallest first and largest value will be the last one.
โ The sort method "typecasts" the elements into strings and compares the strings to determine the order of the elements.
๐Ÿ’ก Example 1:
โ Suppose, if we want to sort only the "small" characters of the array, it will print as expected.
Right?๐Ÿ˜Š
๐Ÿ’ก Example 2:
โ Now, if we want to sort only the "capital" characters of the array, it will also print the expected result.
Right?๐Ÿ˜Š
๐Ÿ’ก Example 3:
โ“But what happens if we sort through the mixed cases of the characters?๐Ÿค”
โ˜น๏ธ This time it's returned with an unexpected result.
Right?๐Ÿ˜Š
โ“Who is the culprit here? Let's see an example. Then we will see the work behind the scene.
๐Ÿ’ก Example 4:
โ Suppose we want to sort an array that contains the number of elements.
โ˜น๏ธ But this time it's also returned with an unexpected result.
Right?๐Ÿ˜Š
Let's find out the culprit...๐Ÿ˜Š
๐Ÿค” Culprit Of Example 3:
โ Actually, it is working just fine because it compares the ASCII(unicode value) of the letters.
๐Ÿ‘€ Have a look at the code snippet๐Ÿ‘‡
โ The character code of "E" is the smallest in the array. That's why it will be sorted first, then U(85), and so on.
๐Ÿค” Culprit Of Example 4:
โ The above logic also applies here, so the character code of 0(48) is the smallest in the array. That's why it will sort first, then 1000(49), and so on.
๐Ÿค” But what is the solution?...
๐Ÿ’ก Solution: "compareFunction"
โ Yes, to resolve this problem we need to pass a "compareFunction" to the sort() method.
โ The sort( ) method accepts an optional argument which is a "compareFunction" that compares 2 elements of the array.
โ But, if we omit the "compareFunction" the sort() method sorts the elements on the basis of Unicode values.
โ The sort( ) method sorts elements on the basis of the returned value of the compare function.
๐Ÿ’ก Syntax of compareFunction:
โ The compare function also accepts 2 arguments, which are 2 continuous elements of the array.
โ Here, "a" and "b" are the 2 continuous elements of the array.
๐Ÿ’ก Solution of Example 3:
1. We need to convert all elements in the same case.
2. Check,
๐Ÿ“Œ if "charCode of a" > "charCode of b" then return any positive value.
๐Ÿ“Œ if "charCode of a" < "charCode of b" then return any negative value.
๐Ÿ“Œ if same then return 0 means nothing.
Still do not understand?๐Ÿ˜Š
Don't worry...
Just say "AAG LAGA DENGE AAG" ๐Ÿ”ฅ๐Ÿ˜‰
๐Ÿ’ก Solution of Example 4:
โ a = 1st item
โ b = 2nd item
๐Ÿ“Œ Just we have to check:
โ if a > b = yes, then we need to SWAP, that's why we have to pass any +ve value.
โ if b > a = yes, then we don't need to SWAP, that's why we have to pass any -ve value.
โ else a === b, it will be the same number, we don't need to do anything.
๐Ÿ˜Š That's all ladies and gentlemen, that's it ๐Ÿง‘โ€๐Ÿ’ป
๐Ÿ’ก Refactor the solution of Example 4:
Suppose,
๐Ÿค” a = 20 , b = 1000
โ If I subtract both of them, a - b = A negative number, right?
Now,
๐Ÿค” a = 1000, b = 500
โ If I subtract both of them, a - b = A positive number, right?
And,
๐Ÿค” a = 500, b = 500
โ If I subtract both of them, a - b = 0, right?
๐Ÿง  So, can I replace the above logic with ๐—ฎ - ๐—ฏ?
Yes, we can do it ๐Ÿ˜Š๐Ÿ‘‡
โ Finally, if you want to sort an array with descending order then you have to only change the order of the return value with ๐—ฏ - ๐—ฎ
๐Ÿ’š I hope you like my way of presenting the topics, so in return, I need your support by just following me and sharing it with your audience.
That's all for now, we will meet in the next thread๐Ÿ˜
๐Ÿ”” Follow Me @ATechAjay
For:
๐ŸŒ Web Development
โœจ JavaScript
โš›๏ธ React JS
๐Ÿ“ Writing Skill
๐Ÿ”ฅ Motivation
๐Ÿ’น Growth
โ›” But Not For Only Resources & Shitpost ๐Ÿ˜
Thank you so much for staying to the end of this thread๐Ÿ’š

Loading suggestions...