Custom Array Sort Algorithms in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends today i'll be covering custom sort algorithms in javascript javascript's array.sort defaults to a string sort and this can catch people off guard when they try and sort an array of numbers let me show you what i mean here i have an array of numbers and you can see that 10 and 15 are actually in between 1 and 3. and even though my array consists of only numbers the sort algorithm is going to convert all of those numbers into strings and then perform a string sort on each one and this may seem kind of strange at first but then when you consider that javascript could have an array that looks like this where you have a mixture of numbers strings and even objects that there's no real way that javascript could figure out how to sort the array properly so that's why it defaults to a string sort and this is where the custom sort function comes in so we can create any function to be a comparison function that takes two items and then compares them and the way that it works is we're going to return negative one if a is less than b we're going to return one if a is greater than b and then we return zero if both of the items are equal so i'll demonstrate this by creating a compare numbers function and i can actually write this very easily as a minus b so when they're equal it'll return zero if a is less than b it'll return negative one otherwise it'll return one and then i'll have to do is take this function and pass it into my sort algorithm and now that sort is using our compare numbers algorithm i can see that everything's sorted correctly and we can create some fairly complex sorts so let's say i wanted to take this mock customer data and sort it by the amount of orders so what i want to do is return this one first second and third because it has one order two and three orders now we just have to create a custom function so const compare order length and again that's going to take an a and a b and what i'm going to do is i'm going to take a dot orders dot length minus b dot orders dot length and to do the sorting i would just have to take customers.sort and pass it my function now if i look i can see that it's sorting in the correct order so let's look at something just a little bit more complicated this example actually comes from a real world use case so this is an example of a response i got back from an api where everything is actually a string even though it does contain a couple of numbers those numbers were being passed back as strings and the business requirement on this were to sort all of the numbers first and then all of the strings second from the code's perspective everything's a string but the business doesn't care they wanted all of the numbers sorted first by number order and then all of the strings second by string order and to create this the algorithm actually wasn't that bad first i just needed a function to detect numbers so i call that is numeric and what that does is it takes a number and i can check to see is nan number and what his number will do is it's going to return true on any of these number strings that are coming back and it will return false on the strings so now i can write the custom compare function and that is still going to take an a and b just like all the other functions now i have a couple of different use cases here one is if a is a number and b is not a number then i'm going to return negative 1 and that's because i want the numbers to have a higher priority than strings and vice versa so if a is a string and b is a number i'm going to return one and then we can test if they're both numbers i can just return a minus b and if it hasn't matched any of these i can just return a is less than b then we will return negative one otherwise one and this works with strings so uh this will work perfectly for what we've got so now let's sort response and see what this is going to look like and there we have it all the numbers are first sorted in number order and the strings are second sorted in string order now of course this isn't the most efficient way right we should probably be caching is numeric in a value inside of the function instead of calling it this many times but this function is just for demonstration purposes on what you can do with sorting so i'll leave those optimizations up to you so i'd like to thank you for making it to the end of the video i really do appreciate you spending your time with me and just a reminder when subscribing you've got to click the bell also or you won't get notifications when new videos come out and as always thanks for watching and i'll see you in the next video [Music] oh [Music] oh [Music]
Info
Channel: Joel Codes
Views: 2,723
Rating: undefined out of 5
Keywords: javascript, nodejs, programming
Id: -S8b_j5XBV0
Channel Id: undefined
Length: 5min 56sec (356 seconds)
Published: Thu Oct 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.