Why javascript sort gets so many memes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this game is just absolute crap isn't that what happens this is the usual case scenario when we really don't understand some things we either get afraid of it or we just like to make fun of it in the case of javascript when you see scenarios like this we chose to make fun of it because we don't understand what's happening behind the scene and to be honest i enjoy these memes too in this particular scenario a usual first i look gives an impact that javascript is trying to be very inconsistent but this is just the opposite of what javascript is trying to do here javascript is trying to be as much as consistent as possible and this is the price you pay for being too much consistent in most programming languages when you just see these sort methods you just like to just assume that these sort methods will sort everything in what you really like to do probably in ascending or descending order but javascript is going one step further in trying to be more consistent and hencef hence paying the prices here let me walk you through on my computer screen and try to show you that what exactly is happening and what to expect in these special scenarios in javascript so i have written some functions so i'll keep this one as short and we'll have a small discussion on this sort method so when you run this sort method on any array you get some funky results and this is all intentional by javascript and they also mentioned that pretty clearly the sort method uses elements in array in place so that's a little bit saving of the spaces there but it returns the sorted array but not like any other array the default sort is ascending but first and foremost every single element is converted into the string and then the sorting is done and not just the sorting based on comparing the string that's how we do according to the mathematical formulas and rule we have got but here in javascript once every element is converted into string these conversions first and foremost is based on utf-16 and then which element comes first in the table based on that we do the sorting not based on mathematical where uh something like two is smaller than three so we don't follow that one we follow actually which one comes first in the table now of course there is a little bit a discussion that needs to be done on utf-16 why not utf-8 everybody follows utf-8 most of the major website follows utf-8 which was and currently is the major standard utf-16 allows you to have more characters into your uh arena where you are doing everything so it also mentions up here so if you'll notice onto the oracle website here they also clearly mentioned that the utf-16 character encodes brings more character to the picture characters like chinese symbol japanese symbols and korean and you might be wondering i don't use them but not accurately my friend this utf-16 encoding allows you to have not only just these chinese and korean characters but also to include something which we all are a big fan emojis utf-16 encoding allows you to directly include emojis or emoticons however you say that into your pictures and swift is one of the programming language which uses it like insanely everywhere almost everywhere so many games are there too but you can build all of these games here too okay this brings us to the point that okay this is not going according to our regular mathematics so how we can actually manipulate to follow these and this is where this method shines the sort method actually allows you to give it give it a method that you can write customly on your own and can give it so let me just show you that so we'll go up in here so we have this example here first let me bring this sort method up here so we have got the sort method but if i write a code which is i'll not waste your time by showing you this basic of the code we can have this function which says my sort and you can have a and b and now we are comparing that if a is greater than b we return 1 if b is greater than a we return negative 1 or we return a 0 in case both are equal now this is kind of a regular example that we can give and i can just copy this one go up here and we can simply go ahead and paste this one and now we have this method in here now if i just use this one instead of doing sort the regular sort i can pass on my method here so i can simply say i want to give you this my sword and now let's just go ahead and do it now it performs things based on what i expected to do because i have explicitly given a direction to do what i really want to do okay one more small thing here many times you'll see that people don't write this much of a big function you'll see a smaller compact version of it something like a comma b then an arrow function and then a minus b and stuff like that yes that is 100 possible those methods are good if you are doing only number comparison in fact i would recommend if there is a comparison which is only for numbers go ahead and do that but if you wanted to be more explicit about strings and everything then this is the way you might want to go a little bit more to that so what power does it give like this is this looks like an extra work that we are doing yes of course but why is it here this is here that you do some extra work and you understand how powerful this can be let me show you another example here so let's just say this is all what we understand we don't need this now this is a very basic function if you're following up my javascript series this shouldn't be a big deal but let me show you something interesting here so this is the order that we can do something very interesting let's just say you are building a website in which you are keeping all the orders of the game like this is in this series the games came out and somebody comes to you and says that hey i want you to arrange these games only these three games not all of them three in the order in which they were released so what you can do that you can actually define a simple function which says batman game order and we have an a b which will get the comparison and this is my array in which all of the games of the batman are in the particular order in which they came in and then i write a simple method which compares the index of a and b now if somebody comes to me and says hey these are the games that i want you to arrange in the specific order they got their release now notice here this set here this array consists of all the games of batman that were released but in here we don't have all the games some games are missing in here and they're also jumbled up in the order so now if you use the games.sort on this array it will sort them according to the release order they were released but it's also not worried about those the games which are missing here so if i just copy and run this you'll get the point that how this is all being done so let's clear this one paste that and yes of course we get all the release dates here so our origins then arkham origin black gates then directly we move on to arkham asylum and arkham knight but notice here the original order was after the black gate there was assault on arkham then arkham asylum then city and then the knight so this is the original order but my user who came up with only selected games is not worried about whether he's passing on all the games or not so this sort method can be really really powerful and the best part that i like about it is that it is very very customizable but again again if you want to make some more memes on that i'll be happy to know more of them so share it with me too and yeah just one more thing if this video doesn't get a like and a subscribe i don't know what gets that probably i should start a reaction channel or maybe a vlog channel so welcome to cycle vlogs because i cannot afford a motorcycle so welcome to bicycle vlogs [Music] you couldn't keep the door shut and now the frostbite's creeping in cause i've been stone cold
Info
Channel: Hitesh Choudhary
Views: 21,171
Rating: undefined out of 5
Keywords: Programming, LearnCodeOnline, javascript sort, javascript, learn js, learn javscript, javascript free tutorials
Id: u7NFUX62-cM
Channel Id: undefined
Length: 8min 21sec (501 seconds)
Published: Tue Sep 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.