A complete thread on sort( ) method in JavaScript:๐งต
[2, 100, 70, 50].sort( )
โฉ
[100, 2, 50, 70]
Why?๐ญA
โฉ
[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...๐ฅ
๐ฏ 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.
โ 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.
โ The sort method "typecasts" the elements into strings and compares the strings to determine the order of the elements.
๐ก 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.
โ 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.
โ The sort( ) method sorts elements on the basis of the returned value of the compare function.
Still do not understand?๐
Don't worry...
Just say "AAG LAGA DENGE AAG" ๐ฅ๐
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.
โ 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.
๐ก 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?
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?
โ 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.
๐ 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๐
๐ 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...