Learn JavaScript SORTING in 6 minutes! 🗃

  Рет қаралды 25,301

Bro Code

Bro Code

Күн бұрын

// sort() = method used to sort elements of an array in place.
// Sorts elements as strings in lexicographic order, not alphabetical
// lexicographic = (alphabet + numbers + symbols) as strings

Пікірлер: 42
@BroCodez
@BroCodez 11 ай бұрын
// sort() = method used to sort elements of an array in place. // Sorts elements as strings in lexicographic order, not alphabetical // lexicographic = (alphabet + numbers + symbols) as strings // ---------- EXAMPLE 1 ---------- const numbers = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6]; numbers.sort((a, b) => a - b); //FORWARD numbers.sort((a, b) => b - a); //REVERSE console.log(numbers); // ---------- EXAMPLE 2 ---------- const people = [{name: "Spongebob", age: 30, gpa: 3.0}, {name: "Patrick", age: 37, gpa: 1.5}, {name: "Squidward", age: 51, gpa: 2.5}, {name: "Sandy", age: 27, gpa: 4.0}] people.sort((a, b) => a.age - b.age); //FORWARD people.sort((a, b) => b.age - a.age); //REVERSE people.sort((a, b) => a.gpa - b.gpa); //FORWARD people.sort((a, b) => b.gpa - a.gpa); //REVERSE people.sort((a, b) => a.name.localeCompare(b.name)); //FORWARD people.sort((a, b) => b.name.localeCompare(a.name)); //REVERSE console.log(people);
@alexkiiru1283
@alexkiiru1283 8 ай бұрын
Thank you dude. Lol You did squidward dirty giving him a gpa lower than spongebob
@dumptruck3354
@dumptruck3354 7 ай бұрын
lol
@oshione7873
@oshione7873 6 ай бұрын
This guy is such a clutch
@omar5695
@omar5695 26 күн бұрын
Thank you! I was getting the difference between lexicographic and alphabetical
@JamieDawsonCodes
@JamieDawsonCodes 5 ай бұрын
1:40 - Thanks for explaining the a - b thing in sort(). I didn't understand what was going on under the hood.
@raniv5132
@raniv5132 4 ай бұрын
Still,I could nt understand this thing ..could yu explain with example?
@JamieDawsonCodes
@JamieDawsonCodes 3 ай бұрын
@@raniv5132 Sure thing! Here's what's happening: If the return value is negative, a should come before b (i.e., a is less than b). If the return value is positive, a should come after b (i.e., a is greater than b). If the return value is zero, the order of a and b remains unchanged with respect to each other.
@latostadaa
@latostadaa 3 ай бұрын
Thank you, you made this easy to understand
@code-with-jimy
@code-with-jimy 5 ай бұрын
Thanks brother this video is very easy to understand
@JadeWays
@JadeWays 5 ай бұрын
Thank You! you make it easy to understand.
@hunin27
@hunin27 11 ай бұрын
are these old videos or are they new?
@BroCodez
@BroCodez 10 ай бұрын
They're new but unlisted since I still need to create thumbnails
@hunin27
@hunin27 10 ай бұрын
ok ok. thanks!@@BroCodez
@peterarcuri3291
@peterarcuri3291 29 күн бұрын
As always, thank you bro code!!
@mr.joecool
@mr.joecool 8 ай бұрын
very cool, thanks for sharing!
@khansikki
@khansikki 4 ай бұрын
i want re-sort object according to name preserving keys //js const people = {1:{name:'carole',age:13},2:{name:'bob',age:12},3:{name:'alice',age:11}} //required output people = {3:{name:'alice',age:11},2:{name:'bob',age:12},1:{name:'carole',age:13}}
@monayzah
@monayzah 8 ай бұрын
Thank you!
@dennistaranchuk4198
@dennistaranchuk4198 5 ай бұрын
Life saver
@vallunacoder.wecodetogether
@vallunacoder.wecodetogether 8 ай бұрын
hey Bro thanks for your videos, can you make some projects to understand how to use all this JavaScript methods for a website ?
@AnkitTechTalks
@AnkitTechTalks 5 ай бұрын
Thanks, ❤ from India
@janosjanosi1148
@janosjanosi1148 Ай бұрын
Hey Bro! Thank you! Can I keep the original order? And sort into another array?
@LearnwithRandomYoutuber
@LearnwithRandomYoutuber 7 ай бұрын
Why sort method works for only numbers below 10 ? Like wise in example apart from using Arrow function
@ArconicTower
@ArconicTower 6 ай бұрын
if you sorted words "az" and "b", az is first because "a" is before "b" if you sorted numbers "10" and "2", 10 is first because the "1" in 10 is before "2"
@rgraptor2542
@rgraptor2542 6 ай бұрын
I'm pretty sure it works for numbers greater than ten? ((a, b) => a - b) In another video a guy explains that it subtracts b from a (in this case) and if the answer is greater than 0, b comes first. If the answer is less than 0, a comes first. so ((a, b) 1324 - 549) the answer is 775 which is greater than 0 so b comes first ((a, b) 342 - 1293) the answer is -951 which is less than so a comes first
@malablondi77
@malablondi77 4 ай бұрын
this helped in understanding how sort() works but i need to sort the outputs of mathemetical functions , how do you do that?
@allhailalona
@allhailalona 4 ай бұрын
i would think squidward is more intelligent than spongebob
@shannonstumpf1861
@shannonstumpf1861 17 сағат бұрын
that's what a squidward would think lol
@yuvrajsingh-fz7hd
@yuvrajsingh-fz7hd 2 ай бұрын
just subscibed thanks again
@yuvrajsingh-fz7hd
@yuvrajsingh-fz7hd 2 ай бұрын
thanks man
@shieldanime6968
@shieldanime6968 8 ай бұрын
so the parameter a is the the first and the parameter b is the second.. can we switch the two parameter so that b is the first and a is the second? just like this? in the number.sort((b,a) => b - a)
@BrahimBrahim-vs5np
@BrahimBrahim-vs5np 7 ай бұрын
yep it the same as number.sort((b,a) => b - a) it is just a naming
@noah_ayyubi
@noah_ayyubi Ай бұрын
u didnt explain the reason why tho
@tanviranjum695
@tanviranjum695 4 ай бұрын
What’s the complexity of this sort() method
@theMadZakuPilot
@theMadZakuPilot 6 ай бұрын
great vid bro.
@kathikr9360
@kathikr9360 7 ай бұрын
thank you
@kathikr9360
@kathikr9360 6 ай бұрын
thanks again
@Drelka
@Drelka 7 ай бұрын
thanks bro
@Blitz61wasd
@Blitz61wasd 10 ай бұрын
Cool
@Blitz61wasd
@Blitz61wasd 10 ай бұрын
Thanks for like
@tauhidxahmed
@tauhidxahmed 8 ай бұрын
hey bro
How to SHUFFLE AN ARRAY in 4 minutes! 🔀
4:05
Bro Code
Рет қаралды 11 М.
Learn JavaScript CLOSURES in 10 minutes! 🔒
10:58
Bro Code
Рет қаралды 20 М.
The Joker wanted to stand at the front, but unexpectedly was beaten up by Officer Rabbit
00:12
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 21 МЛН
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
Двое играют | Наташа и Вова
Рет қаралды 2,6 МЛН
JavaScript Comparator Function | Sorting Explained!
12:21
The Code Creative
Рет қаралды 33 М.
JavaScript DESTRUCTURING in 8 minutes! 💥
8:41
Bro Code
Рет қаралды 21 М.
Array Methods in JavaScript | 17 Useful Methods
42:39
DoableDanny
Рет қаралды 57 М.
sort Array Method | JavaScript Tutorial
5:47
Florin Pop
Рет қаралды 141 М.
16.9: Array Functions: sort() - Topics of JavaScript/ES6
9:19
The Coding Train
Рет қаралды 166 М.
Build this JS calculator in 15 minutes! 🖩
15:20
Bro Code
Рет қаралды 586 М.
Learn JavaScript CALLBACKS in 7 minutes! 🤙
7:17
Bro Code
Рет қаралды 55 М.
Learn JavaScript OBJECTS in 7 minutes! 🧍
7:01
Bro Code
Рет қаралды 61 М.
How are holograms possible? | Optics puzzles 5
46:24
3Blue1Brown
Рет қаралды 275 М.
The Joker wanted to stand at the front, but unexpectedly was beaten up by Officer Rabbit
00:12