This is by far my favourite coding channel on UA-cam. Clear concise instructions and a worksheet to engage with. Thanks James have an amazing Thursday.
@@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 ❤😀
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?
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'
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 < b.gender) return -1; if (a.gender > b.gender) return 1; if (a.name < b.name) return -1; if (a.name > b.name) return 1; }); console.log('----- Sorted by Gender -----'); console.log(byGender);
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] < b[property]) return 1; 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.
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.
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
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".
This is by far my favourite coding channel on UA-cam. Clear concise instructions and a worksheet to engage with. Thanks James have an amazing Thursday.
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!
Very easy to understand. Thank you for doing examples with numbers and strings.
James, these are great short vids. I love doing the short exercises.
Glad you’re enjoying it. Any other topics you’d like to see?
I loved that there were different types of examples. Thank you
All this videos have helped me so much, i really appreciate them
I love how you have the repo for these lessons. Keep it up man you're killing it
Yaaaaa! :)
Again an amazing value packed video! Thank you!
Keep doing what u do, great mann!
Hello James, thanks and very good job, from France !
Thank you. You teach amazing!.
Thank you James! Very cool and understandable tutorials ! I got the idea of array` s methods only from your videos!
Wow...Javascript series ❤❤
Really excited for this series
Hope you’re enjoying it so far!
@@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 ❤😀
@@indian_gagan This means a lot! :) Thank you!!
Didactically very well produced and presented. Great Work!
Thank you James! Very cool and understandable tutorials !
Thank you for great explanation.
thank you James for sharing knowledge.
small suggestion, you could've used a console.clear() at the start of the file so following the console would be a little clearer
That’s a great idea!
I finally understand it, thank you!
what are you saying you are running at 2:25? Did you say node mod? trying to get that extension but cant find it
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)
I mean you just create a blank index HTML file, put a script file inside HTML, and start live server. Works perfectly.
Thanks
Thank you
Excellent
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?
Great question. They have hidden mathematical values actually. Each character has a number code behind it so they can be sorted mathematically.
@@JamesQQuick "character" being, in this case the letters forming each character's name!
So helpful
Better than GOT for an eager heart to learn
how do i get the terminal to work
how do u display each object in a card. ?
I want to be great at javascript :)
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'
What to do if we need to sort by gender and name?
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 < b.gender) return -1;
if (a.gender > b.gender) return 1;
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
});
console.log('----- Sorted by Gender -----');
console.log(byGender);
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] < b[property]) return 1;
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.
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.
isn't anakin and darth vader the same character?
btw .sort() does not return a new array, it returns the original for convenience.
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
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".
Fair enough. Ya it doesn’t really matter what you return but always good to reference the docs.
In his examples, he either returning 1 or -1. He never returned undefined.
instead of doing return twice, you should better use this ------> const byName = characters.sort((a, b) => a.name < b.name ? -1 : 1);
What about Dates it’s confusing
Take a look at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare for sorting strings.
you did everything except explaining how the sort method works