JavaScript Array Sort Method Practice in 5 Minutes
Learn to use the Sort() function to customize how an array is sorted.
Arrays are an essential part of JavaScript. If you want to get better at JavaScript, you have to know how to effectively use arrays, especially the built-in array methods like map, reduce, filter, sort, every, some, etc. In this series, we'll cover the JavaScript array methods you need to know to become a web developer.
Array Worksheet - bit.ly/jqq-array-practice
Join the 💬 Discord Server 💬 - / discord
_____________________________________________
Newsletter 🗞
Interested in exclusive content and discounts? 🤯 Sign up for the newsletter!
www.jamesqquick.com
_____________________________________________
Connect with me 😀
Live streams on Twitch - / jamesqquick
Follow me on Twitter - / jamesqquick
Join the 💬 Discord Server 💬 - / discord
_____________________________________________
COURSES 💻
Learn how to build Fullstack apps with React and Serverless Functions - www.jamesqquick.com/courses/r...
Learn everything you need to know about Visual Studio Code - www.udemy.com/learn-visual-st...
Build a Quiz App - www.udemy.com/build-a-quiz-ap...
Пікірлер: 55
This is by far my favourite coding channel on KZread. Clear concise instructions and a worksheet to engage with. Thanks James have an amazing Thursday.
@JamesQQuick
3 жыл бұрын
Wow that’s like the best compliment I’ve ever gotten! :)
This repository was amazing. I am starting a coding bootcamp in a few days and brushing up on this makes me feel WAAAAY more confident. Thanks!
Again an amazing value packed video! Thank you!
Very easy to understand. Thank you for doing examples with numbers and strings.
Thank you James! Very cool and understandable tutorials !
All this videos have helped me so much, i really appreciate them
Thank you James! Very cool and understandable tutorials ! I got the idea of array` s methods only from your videos!
Didactically very well produced and presented. Great Work!
I loved that there were different types of examples. Thank you
Keep doing what u do, great mann!
thank you James for sharing knowledge.
I finally understand it, thank you!
Thank you. You teach amazing!.
Thank you for great explanation.
Hello James, thanks and very good job, from France !
James, these are great short vids. I love doing the short exercises.
@JamesQQuick
3 жыл бұрын
Glad you’re enjoying it. Any other topics you’d like to see?
I love how you have the repo for these lessons. Keep it up man you're killing it
@JamesQQuick
3 жыл бұрын
Yaaaaa! :)
Excellent
So helpful
Thank you
small suggestion, you could've used a console.clear() at the start of the file so following the console would be a little clearer
@JamesQQuick
3 жыл бұрын
That’s a great idea!
Thanks
Wow...Javascript series ❤❤ Really excited for this series
@JamesQQuick
3 жыл бұрын
Hope you’re enjoying it so far!
@ind_gagan1
3 жыл бұрын
@@JamesQQuick you are someone who is helping young developers a lot....big thanks to you...im currently learning frontend web development and its so amazing and your videos are huge support for us. Thankyou so much James ❤😀
@JamesQQuick
3 жыл бұрын
@@ind_gagan1 This means a lot! :) Thank you!!
Better than GOT for an eager heart to learn
how do u display each object in a card. ?
I want to be great at javascript :)
how do i get the terminal to work
New to coding here. I don't understand how sort by name works. How does the conditional understand that Anakin is alphabetically smaller than Darth? Do the strings have hidden alphabetical values?
@JamesQQuick
2 жыл бұрын
Great question. They have hidden mathematical values actually. Each character has a number code behind it so they can be sorted mathematically.
@vladrileynavilys
2 жыл бұрын
@@JamesQQuick "character" being, in this case the letters forming each character's name!
what are you saying you are running at 2:25? Did you say node mod? trying to get that extension but cant find it
@Hacking-NASSA-with-HTML
Жыл бұрын
I believe it's a Nodemon, live updating your js file; something like "live server" extension for VSCode editor which works with html files (I use it)
@Hacking-NASSA-with-HTML
Жыл бұрын
I mean you just create a blank index HTML file, put a script file inside HTML, and start live server. Works perfectly.
instead of doing return twice, you should better use this ------> const byName = characters.sort((a, b) => a.name < b.name ? -1 : 1);
those who are looking for james's github page which he might forgot to put in his video's description data page link :- github.com/jamesqquick/javascript-array-functions-practice
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'
btw .sort() does not return a new array, it returns the original for convenience.
You need to return 0 when the sort value is the same. I guess returning nothing works the same way (undefined), but the docs are explicit about returnin 0, so that's how it should be done if you want to do it "properly".
@JamesQQuick
3 жыл бұрын
Fair enough. Ya it doesn’t really matter what you return but always good to reference the docs.
@soniablanche5672
3 жыл бұрын
In his examples, he either returning 1 or -1. He never returned undefined.
isn't anakin and darth vader the same character?
What to do if we need to sort by gender and name?
@vmag77
3 жыл бұрын
This is how i did it, but i'm sure there is probably a better way. const byGender = characters.sort((a, b) => { if (a.gender if (a.gender > b.gender) return 1; if (a.name if (a.name > b.name) return 1; }); console.log('----- Sorted by Gender -----'); console.log(byGender);
@soniablanche5672
3 жыл бұрын
Assuming you want to create a general comparator function, you can do something like this. Keep in mind that it will compare the properties directly so you'd have to modify the logic of comparison to suit your needs: function comparator(...properties) { return function(a,b) { const property = properties[0]; if(a[property] > b[property]) return -1; if(a[property] if(properties.length > 1) return comparator(...properties.slice(1))(a,b); return 0; } } characters.sort(comparator("gender","name")); this should sort by gender, and if gender is equal then it will compare the names. You can keep adding parameters to the comparator function if you want to sort by even more properties.
@TomS-rv8me
2 жыл бұрын
This also works: const sortGenderName = characters.sort((a,b) => `${a.gender}${a.name}` > `${b.gender}${b.name}` ? 1 : -1); console.log(sortGenderName); For anything more complicated, the function approach described above would probably be better.
Take a look at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare for sorting strings.
What about Dates it’s confusing
you did everything except explaining how the sort method works