Hi @florin pop. I am no clear about the compare function you put inside sort method. How the comparing is done to sort all numbers because the compare function has a and b which are two parameters? Does sort method take number at index 0 and compare to at index 1, etc?
Yes. It takes all the pairs starting with 0 and 1, then 1 and 2 and so on until it’s sorted. It might even go back on testing 0 and 1 again, but I’m not sure 100% which algorithm is used in JS for sorting.
@@nitishkumargarikapati7767 > 0 sort a after b < 0 sort a before b === 0 keep original order of a and b Now subtract the values in arrays considering the first value as 'a' next value as 'b' one by one, if the subtraction returns positive integer i.e. >0 it'll place the value 'b' first in the array or if it returns
@@sauldownbadman876 This is just the convention that the sort() method follows in JavaScript, and it's important to keep in mind when writing comparison functions for sorting arrays of numbers or other types of elements.
Super helpful tutorial. I was trying to sort arrays of numbers and couldn't figure out why it wasn't working as expected and what exactly happened when you told sort to use 'a-b'. You gave me all the info I needed. Thank you!
"a" and "b" are variables that stand for any 2 given numbers in the array. The .sort() method runs through every element of the array. When you pass a function which contains a - b in it, you're telling the sort method to go over the entire array and subtract index[0] from index[1], then index[2] - index[3] etc. Where the confusion exists for many is that the .sort method checks whether the output of that subtraction is less than 0 or not (we don't write that code, it's included in .sort, hence what makes it a method.) The sort method knows which number is bigger because if you subtract 3 - 6 (a - b) you will get -3. That means that b is bigger than a. In order for the result to be positive it would have to be 6 - 3 (a - b) which equals 3. Therefore the number to the right is smaller than the number to the left and is therefore moved one position closer to the beginning. The .sort has an internal algorithm which when passed (a-b) subtracts all the elements of the array with each other to determine which number is larger. It determines this by seeing whether the output of each subtraction it performs is positive or negative. If the result is negative then b must be bigger than a, therefore it must move closer to the beginning.
how did you get the output on the right hand side of vs code? have tried everything but i can only ues a browser to print out my console. Have tried installing all the extensions on vs code but it doesn't work
In your example you said a is 10 and b is 5. I thought those letters could be represented as; current, previous. Where 5 is current and 10 is previous, and that'd be -5. Hence, 5 would be sorted first. Anyways, I appreciate the video, it helped me to understand some things. God bless! Correct me if I'm wrong tho.
Actually a is the current and b is the next, or if you will a is the previous and b is the current. In any case it's in the same order as you write "(a, b)"!
if I have -> const products = [ {name: 'laptop', weight: {n_weight: 2, g_weight: 2.3}}, {name: 'desktop', weight: {n_weight: 5, g_weight: 5.6}} ] how to sort n_weight or g_weight
Hello, new to JS, and I put this code in without the function and it sorted the numbers automatically. Is this a new function added since this video came out? const numbers3 = [12, 87, 23, 42, 33, 99, 43]; numbers3.sort(); console.log(numbers3);
not neccesarily, this is as clear and straightforward as it could get. Do you want him to teach you from variable assigning? If you cant catch up with him, you need to go back reviewing what you have learned so far. This is like level 2 JavaScript out of 10 level.
Check out the other videos in the JavaScript Array Methods series: ruclips.net/p/PLgBH1CvjOA62PBFIDq55-S6Beivje30A2
how do we sort the name ? You have shown the price only.
Thank you a lot, I was stuck for like 7 hours trying to swap every array in the list. This method is so much simple and cool
simple, short, and very clean explanation I was looking for! thanks!
Man, I thank god that I found you, thanks for tutorial, you saved me a lot of time. Love you man !
Hi @florin pop. I am no clear about the compare function you put inside sort method. How the comparing is done to sort all numbers because the compare function has a and b which are two parameters? Does sort method take number at index 0 and compare to at index 1, etc?
Yes. It takes all the pairs starting with 0 and 1, then 1 and 2 and so on until it’s sorted. It might even go back on testing 0 and 1 again, but I’m not sure 100% which algorithm is used in JS for sorting.
@@FlorinPop thanks. Anyway, I would also think that the method keeps checking until all elements are sorted correctly.
Yes
@@FlorinPop Wouldn't that take a very long time if you'd have many numbers ?
Can you please explain why a comes first when it returns positive value...that's the confusion I have☹️
same doubt
@@nitishkumargarikapati7767
> 0 sort a after b
< 0 sort a before b
=== 0 keep original order of a and b
Now subtract the values in arrays considering the first value as 'a' next value as 'b' one by one, if the subtraction returns positive integer i.e. >0 it'll place the value 'b' first in the array or if it returns
@@Ii-fo8pq still doesn't make any sense. Does a and b change? Like if a goes before b then does b become the new a?
@@sauldownbadman876 This is just the convention that the sort() method follows in JavaScript, and it's important to keep in mind when writing comparison functions for sorting arrays of numbers or other types of elements.
@@Ii-fo8pq thanks, I had no clue what he meant by 'comes first'
dude i have no words like what... you have no idea the wierd explanations I saw for this and you made it so easy. thank you
This is amazing florin. I follow you on twitter but this is the first time I came here. I finally understand this function
Happy to hear that 😃
if(array.length > 2) a = current; b = next;
.sort() method is looping over the array
I hope you gets it, this is the simplest way I could explain it
this is for the people confused what a and b are for
Super helpful tutorial. I was trying to sort arrays of numbers and couldn't figure out why it wasn't working as expected and what exactly happened when you told sort to use 'a-b'.
You gave me all the info I needed. Thank you!
Glad it helped ☺️
great video and a good explanation for self-learner. too much appreciated.
Finally understood. Read shit loads of blogs but nowhere was that greater and smaller condition mentioned properly, all of them just stated condition.
Explanation was short and sweet . Frankly speaking , I got the main point .
That's the goal! 😁
Thank you so much. I was stuck on this sort() method. You made things easy.
Natural teacher!
First part of this series that I'm completely lost 😂
Thank you for showing descening and ascending fantastic video
How to sort mix array where it has number, letter and emoji symbol .?
[“q”, 9,5,🤪, 🤨1,6,3,8,😊 , “a” “g”]
I was stuck in this thing. This really cleared things up.
javascript is prety cool. Also, thanks for the explanation, very clear and concise.
How would it pick up the values of 'a' and 'b' from the array?
"a" and "b" are variables that stand for any 2 given numbers in the array.
The .sort() method runs through every element of the array. When you pass a function which contains a - b in it, you're telling the sort method to go over the entire array and subtract index[0] from index[1], then index[2] - index[3] etc.
Where the confusion exists for many is that the .sort method checks whether the output of that subtraction is less than 0 or not (we don't write that code, it's included in .sort, hence what makes it a method.)
The sort method knows which number is bigger because if you subtract 3 - 6 (a - b) you will get -3. That means that b is bigger than a. In order for the result to be positive it would have to be 6 - 3 (a - b) which equals 3. Therefore the number to the right is smaller than the number to the left and is therefore moved one position closer to the beginning.
The .sort has an internal algorithm which when passed (a-b) subtracts all the elements of the array with each other to determine which number is larger. It determines this by seeing whether the output of each subtraction it performs is positive or negative. If the result is negative then b must be bigger than a, therefore it must move closer to the beginning.
how did you get the output on the right hand side of vs code? have tried everything but i can only ues a browser to print out my console. Have tried installing all the extensions on vs code but it doesn't work
Thank you so much for the clear explanation!
Now, I understand what sort is used for arrays. Thanks!
Thank you for making the video it helped me a lot while i was confused.
Hello Florin ! My Question is How i Find Third Largest Value Using set, sort and reverse in JavaScript ?
Yeah... if we need to sort between an object parameter that can have strings or integers, what suggestion could you recommend here?
thank you so much . I was stuck there
I am grateful to you brother for easy explanation.. Thanks very much
Great explanation.
How do you use the terminal as a preview screen?
Thank you mate!
Awesome explanation 👏🏼❤️ Thank you so much! Keep going :)
Thank you so much , this is very clear explanation!
Thank you! just exactly what I was looking for :)
Glad I could help!
very helpful. you have my thanks
thanks a lot for this video!!!!
Made it so clear, thank you!
is it fixed now ot smth? because the .sort() is working for numbers now
great explanation.
Thanks, I was unsure about how things "came first" until I came across this video.
Glad it helped! 😁
In your example you said a is 10 and b is 5. I thought those letters could be represented as; current, previous. Where 5 is current and 10 is previous, and that'd be -5. Hence, 5 would be sorted first. Anyways, I appreciate the video, it helped me to understand some things. God bless! Correct me if I'm wrong tho.
Actually a is the current and b is the next, or if you will a is the previous and b is the current. In any case it's in the same order as you write "(a, b)"!
best explanation, thanks, sir
thankyou, you gain a subscriber
Thank you for the great explanation
Glad it was helpful!
Great video, thanks.
clean and to the point, thank you.
Glad it helped!
Great tutorial, thank you!
Damn I wish I know 10% of Florins JS knowledge! I saw your 10 projects in 10 hours so proud of you. haha.
should it be minus or to? Because it confused me
well explained, thank you sir :)
Awesome. Thank you brother
My pleasure
thank you my friend
Nice, Thank you so much
You are most welcome
Thank you for this.
Thank you so much!)
Excellent. Exactly what I was looking for.
@@lycan2494 my pp is from db super though
Tnx florin was usefull
Gajab Sirg
fun tricks in sorting. Thank you
You're welcome! 😃
thank Bro
good explanation
Nice video sir. It helped me very much to understand the concept. Thankyou very much for making such amazing content like this.
Well explained
how do i turn my visual code like that? it automatically see the output when need to type node js command?
Look into nodemon
How can I sort divs by their id number?
if I have -> const products = [ {name: 'laptop', weight: {n_weight: 2, g_weight: 2.3}}, {name: 'desktop', weight: {n_weight: 5, g_weight: 5.6}} ]
how to sort n_weight or g_weight
Thanks a lot bro
very well explained m ur new subscriber
Thank you😊
I love this
great stuff, reall easy to understand. I am a newcomer. Compare that with MDN page on sorting lol
thank you!
great help thankyou sir
You are welcome!
keep good work up bro
Hey, what to do if I have a symbol infront of numbers in number array, like, [$74, $18, $10, $2, $84, $24, $105]?
Those are strings. In the sort function you’ll have to convert them to numbers and compare it that way
.sort () use bubble sort internally ??????
Got it bro! 👍
Great!
Hello, new to JS, and I put this code in without the function and it sorted the numbers automatically. Is this a new function added since this video came out?
const numbers3 = [12, 87, 23, 42, 33, 99, 43];
numbers3.sort();
console.log(numbers3);
It sorts in only in ascending order
It's deceptively simple!
Thanks
man, love this format. Damn! :D Keep'em going. be careful of le burnout haha
I will be! 😆
Thnx a lot sir
My pleasure to help!
oh man, I don't speak english but your video is so good that I could understand
thank you
What editor are you using here? thanks
VSCode.
Does anyone know what the run time of this sort() method is?
twitter.com/florinpop1705/status/1594600268385357824?s=46&t=iVn_uTF2LYs7nB1sYBhSxQ
what is a and b here . i don't understand it
yea i wish he dug a little deeper here a little confused with the logic here as well
a and b here are the 1st number and 2nd number respectively. Then on the second iteration, it's 2nd and 3rd and so on
@@MaxProgramming ohh got it thank alot
@@zafarhussain8273 Well, maybe I was wrong. It can also be a = 1st, b = 2nd - a = 3rd, b = 4th - a = 5th, b = 1st. If there are 5 items in an array
@@lycan2494 Thank you. I get a littlie confused 😅
Thank you. I had been struggling to sort years in a filter in ascending order. This did the trick.
Thank you sir
Thanks Pop
how do we sort the name ? In video the price is shown only.
products.sort((a, b) => a.name.localeCompare(b.name)); //localeCompare( ) "Determines whether two strings are equivalent in the current locale."
@@jritzeku can you do the same thing with out using .localecompare()
can someone tell how is his terminal automatically updating with console.log("")??
Nodemon
In a nutshell can I say, a - b -> accending order while b - a -> will be descending order ?
I think you can
@@FlorinPop thanks
how do i return a name in an array?
I need more details in order to help you
awesome
Thanks bro
const products = [
{
name: 'laptop',
price: 1000
},
{
name: 'desktop',
price: 1500
},
{
name: 'phone',
price: 500
}
];
you are literally scratching the surface and letting us pressume that the sort() function does all the magic by itself.
not neccesarily, this is as clear and straightforward as it could get. Do you want him to teach you from variable assigning? If you cant catch up with him, you need to go back reviewing what you have learned so far. This is like level 2 JavaScript out of 10 level.
You can also read about it in MDN. Personally I find it a quite straight forward video.
Nice
The number short only shorts the first number