Тёмный

16.9: Array Functions: sort() - Topics of JavaScript/ES6 

The Coding Train
Подписаться 1,7 млн
Просмотров 166 тыс.
50% 1

In this video, I discuss the JavaScript array function: sort().
🎥 Next Video: • 16.10: Particle System...
🔗 MDN's Array Documentation: developer.mozilla.org/en-US/d...
🚂 The Coding Train website: thecodingtrain.com/
💖 Support this channel on Patreon: / codingtrain
🛒 To buy Coding Train merchandise: www.designbyhumans.com/shop/c...
📚 Book recommendations: www.amazon.com/shop/thecoding...
💻 github.com/CodingTrain/Rainbo...
🎥 For an Introduction to Programming: • Start learning here!
🎥 For More Coding Challenges: • Coding Challenges
🔗 p5js.org/
🔗 processing.org
📄 Code of Conduct: github.com/CodingTrain/Code-o...

Опубликовано:

 

22 фев 2018

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 168   
@shilangyu
@shilangyu 6 лет назад
1:10 it did not sort it in numerical order. Array.prototype.sort is not sorting numerically. it threats your numbers as strings, therefore an array [3, 21, 10] would be sorted to => [10, 21, 3]
@TheCodingTrain
@TheCodingTrain 6 лет назад
whoa, just confirmed this is the case! Thanks for the correction!
@shilangyu
@shilangyu 6 лет назад
glad i could help :)
@Sindoku
@Sindoku 6 лет назад
Right, it's doing it with the .charCodeAt() function from what I can tell.
@fr3fou
@fr3fou 6 лет назад
so it's using the ascii value? if yes, that would make sense that it prioritises numbers -> capital letters -> lowercase letter
@ratankhalsa6034
@ratankhalsa6034 5 лет назад
Alright, for those confused like me, here's the explanation. Sort method changes them to '10, '21', '3' and then checks them. So, the number starting with 1 comes first, that starting with 2 comes next and so on. If you had [400, 3, 21, 100000], the output after sorting would be [10000, 21, 3, 400].
@tuanledang6138
@tuanledang6138 5 лет назад
i'm Vietnamese student and i was stuck when i try to study in the internet till i find your channel . I'm not good in English so I meet many struggle to learn the definition of some thing new. But with your explaining I can understand the content easily, coding being funny ,i really interesting with your style in teaching ,I never meet one like u in my country. I watch all of them. thanks bro!!!
@fabioaliu
@fabioaliu 4 месяца назад
hey bro, did you learn coding at last ?
@organizedmarketing
@organizedmarketing 6 лет назад
Actually appreciate your humility and its very helpful watching you code from start to finish.
@erikgrivalsky5493
@erikgrivalsky5493 3 года назад
I really enjoy your videos. When I'm struggling with something on FCC I come to you, and it's a tremendous help. Thanks a lot!
@temitopeakinsoto4499
@temitopeakinsoto4499 4 года назад
I knew I was definitely going to subscribe to this channel a few minutes into this tutorial. Awesome guy, awesome explanation!!!
@rajchavan860
@rajchavan860 3 года назад
Imagine you're in an interview and you laugh in front of the interviewer and say "I always forget whether it is (a-b) or (b-a)"😅😅
@irfangumelar5404
@irfangumelar5404 3 года назад
Did you guys really asked question like that? I'm fortunately never met those kind of interview...otherwise I'd be doomed
@irfangumelar5404
@irfangumelar5404 2 года назад
@@z3rocodes cool, good luck for your interview!
@jiauyjiauy3777
@jiauyjiauy3777 4 месяца назад
Tbh for me it's already enough to know the concepts so you know what to google. Nuances are just so optional to remember
@matekimagick
@matekimagick 2 года назад
This is my favorite video on the internet right now! Thank you ❤
@nikhilsaini3317
@nikhilsaini3317 Год назад
best video i have encounter for comparison function
@soyitiel
@soyitiel 6 лет назад
yes, I've watched all of them
@gabrielcohen4113
@gabrielcohen4113 2 года назад
wondeful videos I am learning more from these videos than from most anythoing else I have tried. I like the way you intuitively merge the concepts with the coding and I also like that you make mistakes and explain them when you find them I plan to get through most in not all of your courses, thank you very much.
@hollym3544
@hollym3544 5 лет назад
Thank you for explaining this like a normal human being. I completely understand now!
@StrangeComments
@StrangeComments 6 лет назад
Loved this whole collection of videos. I'm looking forward to the particle system! That's gonna be fun and come in handy too
@TheCodingTrain
@TheCodingTrain 6 лет назад
(it's actually there already if you look for "next video" in the description!)
@lizavetameta
@lizavetameta Год назад
I was skeptical at first, but now I am in love with ur teaching. I hope, other videos are that useful too.
@lizabeti3457
@lizabeti3457 5 лет назад
I sat and watched all of em and I actually learnt more here then what I could learn from wiki libs...
@MobiusCoin
@MobiusCoin 5 лет назад
I just want to say, you're terrible at teaching those of us who need a quick answer for those of us on a deadline for a particular project for a client BUT you are absolutely fantastic at helping us understand these concepts on a deep level. I've watched a tutorial series on all of these ES6 topics before. I already use ES6 in my work but I've gone through your whole playlist and it feels I actually understand what I'm writing now (also when to use arrow notation, because prior to this series I've just been writing all my functions as arrow functions just because it was fashionable).
@TheCodingTrain
@TheCodingTrain 5 лет назад
Thanks for the feedback!
@jimmychoose215
@jimmychoose215 Год назад
YOU ARE A REALLY GOOD TEACHER. WOW! I'm so glad I stumbled on this.
@alvaromartin6301
@alvaromartin6301 6 лет назад
Thanks for share your knowledge, besides you're a good teacher. Greetings from Peru :D
@bronzebond4869
@bronzebond4869 3 года назад
honestly this is my first time seeing dude, and i am only 1:56 into this video but i get the feeling hes gonna be super helpful from now on. Thanks for all the effort you put into helping random folk online.
@gatorslife22
@gatorslife22 4 года назад
I love how you explain things! SUBSCRIBED!!
@myrinaDM
@myrinaDM 2 года назад
I like your art of teaching. Thank you!
@alzca
@alzca 5 лет назад
Great video, great teacher, great explanations. Thanks (from Spain) for sharing your knowledge!!!
@robertng4666
@robertng4666 4 года назад
thank you. I really like the way you explain the codes.
@frankie_goestohollywood
@frankie_goestohollywood 4 года назад
In the process of watching all of these videos :-)))))) Thank you !!!!!
@niyongaboeric
@niyongaboeric 5 лет назад
I enjoyed your video man. I learned the importance of sort method. Thanks Danny.
@mikegogl
@mikegogl 4 года назад
Man thanks so much. You explained this so insanely good.
@nicolotommasi4811
@nicolotommasi4811 4 года назад
I love your work, thank you so much!
@denzel420
@denzel420 Год назад
The way he explains is just something different, so noob friendly that it is impossible to not understand. I would recommend his lessons to everyone. Even if you know the topic, just refresh your knowledge to make things even more clear in your mind.
@sadhlife
@sadhlife 6 лет назад
It sorts capitals before smalls because in ASCII formatting, numbers come first, capitals next, smalls last. Somewhere in there all punctuation also fits, you can see their ASCII value to know about that.
@loreer123
@loreer123 6 лет назад
i hoped he would have chosen numbers big enough to get sorted between the charaters but he just chose really small ones :(
@TheCodingTrain
@TheCodingTrain 6 лет назад
Yeah, I goofed on this and didn't realize it would not sort numerically.
@blusanders
@blusanders 3 года назад
Love these videos. Still don't quite understand this but love it either way.
@TusharJade101
@TusharJade101 2 года назад
from India 18 learning web dev, Loved the way you teach, If you're our teacher we will be very peacefully and happily become the best developers cause we don't have any good teacher like you are. But, in the future, If I get chance to teach I will copy the way you teaches your amazing sir
@trimski
@trimski Год назад
Eres muy bueno, y gracioso. Hace mas facil querer aprender. Muchas Gracias, desde Argentina.
@nikhilsaini3317
@nikhilsaini3317 Год назад
best video -- i have encounter for comparison function
@oguedoihuchigozirim7257
@oguedoihuchigozirim7257 3 месяца назад
This is Gold. ❤
@williamplay1000
@williamplay1000 6 лет назад
Yay! I watched all of them too😁
@shehrozamin9543
@shehrozamin9543 2 года назад
Amazing explanation God bless you!
@thealexchilders1458
@thealexchilders1458 5 лет назад
You're a lifesaver.
@srinivasaraoyp3640
@srinivasaraoyp3640 Год назад
Very interesting topic. Thank you.
@kieran2347
@kieran2347 3 года назад
Amazing video thanks
@axelgrind
@axelgrind 4 года назад
Helpful video. Thanks :)
@fanofgod9895
@fanofgod9895 2 года назад
Yooo you are the best!❤️
@user-mn3iq2cs9n
@user-mn3iq2cs9n 6 лет назад
I learn coding and also save money on coffee by watching this kick ass channel.
@cryingkelp
@cryingkelp 3 года назад
I love this guy.
@GameMuse
@GameMuse 6 лет назад
You sir, are worth watching . This. This. This. This.! Lol
@TheJpwzrd
@TheJpwzrd 2 года назад
Dude is nuts, subscribed
@francoislecanadien1710
@francoislecanadien1710 3 года назад
The kind of teacher that keep kids in school 👍
3 года назад
Thank you So much , I dont know how many times you saved me or explained me subject !!!!!!!! You are the best my friend, İ'm learning web development , We have a small whatsap group, I like sharing your videos to share knowledge, Please dont stop, You are doing great my friend, İ hope you can show us more including three.js, webGL, react etc...
@heyimanh
@heyimanh 4 года назад
very helpful. thank you
@arindam1249
@arindam1249 4 года назад
This is amazing
@Vittokm
@Vittokm 3 года назад
You are the best !!!
@jenjen3
@jenjen3 6 лет назад
Thank you so much !!
@olusanyaolamide9764
@olusanyaolamide9764 Год назад
I loved how he kept getting it backwards, it means I'm even pro coders struggle sometimes
@vishalsingh-pg6em
@vishalsingh-pg6em 4 года назад
how does the comparing works for the arrow function (a, b) => a - b , how does it do for all array values
@brandon2932
@brandon2932 2 года назад
Would like to know this too. How does it know what a and b is?
@Smthgoingon92
@Smthgoingon92 4 года назад
So the default sort() in javascript uses insertion sort. Do you know if it's still insertion sort if we add a function like in the video?
@roshan3187
@roshan3187 3 года назад
Can you share any updates on this?
@abilash.m6588
@abilash.m6588 3 года назад
Sir can you explain how a function returning -ve value can sort the objects
@opensourceprojects5526
@opensourceprojects5526 3 года назад
how to sort an object only by the name using sort method in js though there are another keys are available
@brucelee7782
@brucelee7782 5 лет назад
How does the sort method knows what to do with the positive or negative values that returns from the compare function? How does it know that it should put the 2nd number at the back if the difference is negative? Wish this video had a tracing of how the .sort(compare) works through an array of numbers like val = [5,3,5,7,8]. Since 5>3, it returns 2, then what does it do next?
@surajpatil8694
@surajpatil8694 2 года назад
so detailed! Indian teacher you tubers must watch this.
@proactivex
@proactivex 5 лет назад
watching this at 1.5 or 1.75 gets me so hyped.
@viniciosfontana
@viniciosfontana 3 года назад
Please, how do I choose 5 assorted numbers of 10 options ? TKS.
@issayasghebru6968
@issayasghebru6968 Год назад
I love it
@ukjoeee
@ukjoeee 6 лет назад
very good!
@jalalbmnf
@jalalbmnf 4 года назад
I want to sort the objects of array as I want, how can I do this?
@agentcodeblade7773
@agentcodeblade7773 3 года назад
You're a good teacher my dude Mashallah
@sumantkanala
@sumantkanala 6 лет назад
Is there a way to do a sort on 2 keys in JS? For example I wanna sort this [[13, 4] , [20, 2] , [7, 7], [4, 4]] using the element in each item as key, but say if both keys are equal as the first and 4th items in the above array are, how would I go about that. I've managed to find this -> www.sitepoint.com/sophisticated-sorting-in-javascript/ but didn't quite understand what's happening here. Would you mind explaining how this works. Thank you!
@LibraryOfTheOligarchs
@LibraryOfTheOligarchs 11 месяцев назад
Appreciate your amazement! lol
@user-eg5cg3lk3e
@user-eg5cg3lk3e Год назад
Can someone please explain the a - b part because I didn't really get it. As far as I understand it subtracts and gets a negative, a positive and zero number of they're equal. But how does it work. Truthy falsely? But negative num is still truthy, false is 0. And does it sort by ASCII order? Thanks!
@dappermink
@dappermink 6 лет назад
Well based on how you have to use the sort function, I assume it works by doing a bubble sort algorithm? or I'm not sure I wonder if using the sort function still is relevant when dealing with extra large arrays :p
@punisherash388
@punisherash388 2 года назад
there should be a thousand like button on this channel on every vide, i don't feel satisfied just after clicking one after watching him code.
@gatin9015
@gatin9015 Год назад
i'm very new to js and this is wrecking my brain. what and how does compare() or sort() link the variables "a" and "b" in this case to the elements within the array? how does two variables (a and b) make an array of more than two elements sort itself in whatever order we choose??
@indraverma2811
@indraverma2811 4 года назад
How alphanumeric sorting is possible in Angular??
@sivajames07
@sivajames07 3 года назад
Great 😃 yaaar
@masterlup
@masterlup 4 года назад
4:49, I came to learn about exactly that :-(
@sinlg
@sinlg 5 лет назад
What about Date functions? when sorting, if one of the parameters in the object was a Date... ( and the format could be varying). like could b DD-MM-YYYY and time, but we only care about date?
@MrRobeezy29
@MrRobeezy29 3 года назад
fantastic
@nsudhir_here
@nsudhir_here 5 лет назад
how to short array of objects of boolean property??
@whitecanary3599
@whitecanary3599 6 лет назад
Hi! I need a program that can disorder songs in a different order. PLEASE SOMEONE HELP ME TO DO THAT. Sounds easy but this is making me crazy
@StarlightHeaven
@StarlightHeaven 6 лет назад
Nice video dan
@TelemetryOverlay
@TelemetryOverlay 6 лет назад
Not sure where I should ask this. Are you planning on doing a tutorial on modules? The different ways these can be handled in JavaScript is confusing. Thanks and keep up the good work!
@TheCodingTrain
@TheCodingTrain 6 лет назад
Please suggest here! github.com/CodingTrain/Rainbow-Topics/issues
@TelemetryOverlay
@TelemetryOverlay 6 лет назад
Done, thanks!
@elithecoder
@elithecoder 6 лет назад
I can't believe THE Daniel Shiffman is amazed by ME!
@anibal9812
@anibal9812 6 лет назад
0:30 Once there was a speedrunner named CarlSagan42, who said "let’s do one more". He never stopped doing just one more. I wonder if this’ll end up like that. ;D Love your explanations, keep up with the amazing work!
@MrZelnoth
@MrZelnoth 6 лет назад
Uno mas!
@nightfury8848
@nightfury8848 2 года назад
Who are the a and b arguments in the compare functions
@brandon2932
@brandon2932 2 года назад
I wanna know too. Curious to know what it considers to be a and b
@1inch69
@1inch69 6 лет назад
What program do you use for programming?
@TheCodingTrain
@TheCodingTrain 6 лет назад
This workflow video might help: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-HZ4D3wDRaec.html
@1inch69
@1inch69 6 лет назад
The Coding Train Thank you very much. Really good explained video. Learned allot. Big thanks. Greetings from latvia.
@papinkelman7695
@papinkelman7695 6 лет назад
I'm not going to sleep before the next video...
@ibuprofenPill
@ibuprofenPill 2 месяца назад
I don't understand the compare thing when sorting in order.
@lucasmedeiros391
@lucasmedeiros391 10 месяцев назад
video dahora brother
@jonnylaw4569
@jonnylaw4569 4 года назад
6:42 ok master yoda lol
@mithunshaw4269
@mithunshaw4269 2 года назад
how to sort each words in an array like was would be aws, and would be adn and so on... first we sort entire array and then how do we sort each words and print them like below //input let str = 'ram ramesh sahil harsh abhay ali gopi' // output -> 'aabhy aehmrs ahhrs ahils ail amr giop'
@matthewvaccaro8877
@matthewvaccaro8877 4 года назад
I wish you went over what happens when you use negative number
@discman15
@discman15 4 года назад
I like your energy dude, you make me laugh. You're like Weird Al if he was from...Lebanon? Cyprus? Keep it up
@MuhammadAdnan2.0
@MuhammadAdnan2.0 3 года назад
Cool
@iNTERS22
@iNTERS22 6 лет назад
Javascript is the best
@mirzaaghaalikhan183
@mirzaaghaalikhan183 3 года назад
Now imma learn Haskell
@MisterDibitybopty
@MisterDibitybopty 5 лет назад
How come "was" (in the array) is before "and" at the end of the video?
@kardashevr
@kardashevr 5 лет назад
'was' appeared earlier than 'and' in the sentence, and javascript didn't care, because it sorted based on length of the word, not based on characters.
@MisterDibitybopty
@MisterDibitybopty 5 лет назад
@@kardashevr Ah ok, makes sense. Thanks!
@rbjassoc6
@rbjassoc6 6 лет назад
got it ;-) console.log(joe.sort((a,b) => a - b)); // sorts array
@brandon2932
@brandon2932 2 года назад
but how does it know what a and b is?
@gokselkucuksahin
@gokselkucuksahin 3 года назад
8:24 *THIS IS THE WAY*
@oliverye5833
@oliverye5833 5 лет назад
why "was" come before "and" if we sort them by length?
@gwivongalois6169
@gwivongalois6169 4 года назад
They use a stable sort internally. ("Stable sort algorithms sort repeated elements in the same order that they appear in the input.")
@subhabrataghoshal2926
@subhabrataghoshal2926 Год назад
@@gwivongalois6169 thanks mate!!
@ashnashilmy1323
@ashnashilmy1323 5 лет назад
@WEBSTART-LIVE
@WEBSTART-LIVE 4 года назад
Thank you. from Russia
@prasadkadu2435
@prasadkadu2435 2 года назад
Probably you might need new whiteBoard or darker ink which would be more clear to see.
@GoutamReddydazz
@GoutamReddydazz 2 года назад
Add ture false in stack and sort true ones above
@noumanmalik960
@noumanmalik960 3 года назад
Below code ouputs to: [ 10, 2, 32, 4, 5, 7, 9] let vals = [5, 4, 9, 2, 10, 32, 7] vals.sort(); console.log(vals); its not sorting I don't know whyy
@subhabrataghoshal2926
@subhabrataghoshal2926 Год назад
If you observe the output carefully you'll see that number starting with 1 came first then number starting with 2 then number starting with 3 and so on. That's how JavaScript shorts by default. Try using a compare function like vals.sort((a,b) => a-b); and it will work just fine.
Далее
JavaScript Comparator Function | Sorting Explained!
12:21
НРАВИТСЯ ЭТОТ ФОРМАТ??
00:37
Просмотров 1,3 млн
16.15: Promise.all() - Topics of JavaScript/ES6
11:44
Просмотров 126 тыс.
Factory Method Pattern - Design Patterns (ep 4)
27:21
Просмотров 539 тыс.
Why javascript sort gets so many memes
8:22
Просмотров 21 тыс.
16.3: ES6 Arrow Function - Topics of JavaScript/ES6
22:32
What was Coding like 40 years ago?
29:05
Просмотров 1,7 млн
Premature Optimization
12:39
Просмотров 774 тыс.
НРАВИТСЯ ЭТОТ ФОРМАТ??
00:37
Просмотров 1,3 млн