Learn JavaScript SORTING in 6 minutes! 🗃

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yo so uh what's up today I'm going to be talking about the sort method in JavaScript the sort method is used to sort elements of an array in place however with the sort method we sort elements as strings in lexicographic order not alphabetical if I could summarize what lexicographic means it means alphabet Plus numbers plus symbols as strings let me give you an example with just alphabetical characters I have an array of fruits to sort this array I can take the array name fruits use the built-in sort method then console.log my array of fruits let me expand this and here's my array all sorted when sorting we treat any elements as strings what if my array were numbers I'll add the numbers 1 through 10 in a somewhat random order let's change our array to be numbers numbers. sort console.log numbers here's the result we have one then 10 then the rest seem to be in order we're not sorting this array numerically we're sorting this array lexor graphically we're treating each of these numbers as strings that's why we have one then 10 10 isn't at the end where we expect it would be there's a couple extra steps we need to do involving sorting numbers inside of the sort method we have to write a custom comparison function this is normally a callback but you can write a function expression or even better yet an arrow function we'll write an arrow function in this case let me zoom in with an arrow function we first need parameters we'll have parameters A and B we'll examine two contiguous elements 1 then 10 10 then 2 two then nine N9 and three so on and so forth then we need an arrow then we'll write a minus B our function a minus B will return either a0 or positive value depending on what values we're examining the sort method will sort those values into place depending on what the value returned is our array should be sorted you can see that 10 is at the end instead of right after one for reverse order you could say B minus a now we're starting with 10 and ending with one you can even sort objects by a given property let's go over that example we will create an array of people const people equals this will be an array will include four objects all comma separated the first will have a name property of SpongeBob age 30 and a GPA GPA 3.0 I'm going to zoom out for the next object just going to put that on a new line let's copy all these properties change name to be Patrick age will be 37 GPA 1.5 again for the next object we will have Squidward age 51 GPA 2.5 then lastly we'll have Sandy name Sandy let's make Sandy 27 GPA of course it's going to be a 4.0 I would like to sort this array of objects by each person's age we'll take our array of objects that's stored within people use the built-in sort method use our Arrow function a comma B Arrow we'll take parameter a that's person one do access their age property minus parameter B access their age property and that's all you got to do now let's console.log R people there we are we have Sandy who's the youngest then SpongeBob Patrick Squidward for reverse order you'll just change a to be B and B to be a Squidward Patrick SpongeBob Sandy or we can sort by a different property let's do GPA this time a. GPA b. GPA we have Patrick with the lowest GPA followed by Squidward SpongeBob Sandy then reverse order would be b. GPA minus a. GPA Sandy SpongeBob Squidward Patrick let's try sorting by the name property and see what happens a.name minus b.n name we have SpongeBob Patrick's Squidward Sandy but that doesn't seem to make sense these aren't in alphabetical order if you need to sort by a property that contains a string within an object there's a different formula we will take a.name do use the built-in local compare method compare to b. name this method will examine two strings for lexicographic order now these are all arranged lexicographically starting with Patrick then Sandy SpongeBob Squidward for reverse lexicographic order we would take b.n name. local compare then examine a.name I've just now realized that there's a lot of SpongeBob characters whose first name starts with an S like we have three right here Squidward SpongeBob Sandy Patrick all right everybody so that is the sort method it's used to sort elements of an array in place it sorts elements as strings in lexicographic order not alphabetical and well everybody that is the sort method in JavaScript
Info
Channel: Bro Code
Views: 17,004
Rating: undefined out of 5
Keywords: javascript, javascript tutorial, javascript full course, javascript course, course, full course, web development, web developer, software engineer, software engineering, coding tutorial, learn to code, tutorial, html, css
Id: CTHhlx25X-U
Channel Id: undefined
Length: 6min 42sec (402 seconds)
Published: Wed Nov 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.