Learn JavaScript SORTING in 6 minutes! 🗃

Поділитися
Вставка
  • Опубліковано 22 січ 2025

КОМЕНТАРІ • 54

  • @BroCodez
    @BroCodez  Рік тому +12

    // 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);

  • @The1stKing
    @The1stKing 27 днів тому +3

    Exactly how a tutorial should be (in my opinion). Short, on point (no unnecessary talking), with relatable (easy to understand) examples. I give this video 10/10. Thank you for making it.

  • @alexkiiru1283
    @alexkiiru1283 Рік тому +20

    Thank you dude.
    Lol You did squidward dirty giving him a gpa lower than spongebob

  • @oshione7873
    @oshione7873 9 місяців тому +2

    This guy is such a clutch

  • @shannonstumpf1861
    @shannonstumpf1861 2 місяці тому

    bro, yer a dang champion!

  • @syuzannastepanyan374
    @syuzannastepanyan374 День тому

    Great job! Admire your JS and language abilities

  • @omar5695
    @omar5695 4 місяці тому

    Thank you! I was getting the difference between lexicographic and alphabetical

  • @JardaniJavanavič
    @JardaniJavanavič 11 місяців тому +1

    very cool, thanks for sharing!

  • @JamieDawsonCodes
    @JamieDawsonCodes 9 місяців тому

    1:40 - Thanks for explaining the a - b thing in sort(). I didn't understand what was going on under the hood.

    • @raniv5132
      @raniv5132 7 місяців тому

      Still,I could nt understand this thing ..could yu explain with example?

    • @JamieDawsonCodes
      @JamieDawsonCodes 7 місяців тому +8

      @@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.

    • @meowmeowkami
      @meowmeowkami 3 місяці тому

      ​@@raniv5132 basically you swap (a, b) if the calculation after that is positive.
      Normal Sorting
      (1, 10) => 1 - 10 --- negative = no swap (normal sorting)
      (2, 1) => 2 - 1 --- positive ( (2,1) becomes (1,2) )
      Reverse Sorting
      (1, 10) => 10 - 1 --- positive = swap
      you manipulated the function from (1 - 10) to = (10 - 1), which gets a positive result, which results in swapping, which result in reverse sorting.
      (1, 10) => 10 - 1 ---- positive = (1, 10) becomes (10, 1)

  • @hunin27
    @hunin27 Рік тому +11

    are these old videos or are they new?

    • @BroCodez
      @BroCodez  Рік тому +6

      They're new but unlisted since I still need to create thumbnails

    • @hunin27
      @hunin27 Рік тому +1

      ok ok. thanks!@@BroCodez

  • @latostadaa
    @latostadaa 7 місяців тому

    Thank you, you made this easy to understand

  • @code-with-jimy
    @code-with-jimy 8 місяців тому +1

    Thanks brother this video is very easy to understand

  • @JadeWays
    @JadeWays 9 місяців тому

    Thank You! you make it easy to understand.

  • @malablondi77
    @malablondi77 8 місяців тому

    this helped in understanding how sort() works but i need to sort the outputs of mathemetical functions , how do you do that?

  • @peterarcuri3291
    @peterarcuri3291 4 місяці тому

    As always, thank you bro code!!

  • @LearnwithRandomYoutuber
    @LearnwithRandomYoutuber 10 місяців тому +1

    Why sort method works for only numbers below 10 ? Like wise in example apart from using Arrow function

    • @user006_SKVN8G6LH
      @user006_SKVN8G6LH 10 місяців тому +3

      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 9 місяців тому +2

      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

    • @ebenezeryeboah7090
      @ebenezeryeboah7090 Місяць тому

      @@rgraptor2542 can you share link to video

  • @bengraham1798
    @bengraham1798 2 місяці тому

    Thank you great video 🎉

  • @monayzah
    @monayzah 11 місяців тому +1

    Thank you!

  • @janosjanosi1148
    @janosjanosi1148 5 місяців тому

    Hey Bro!
    Thank you!
    Can I keep the original order?
    And sort into another array?

  • @AnkitTechTalks
    @AnkitTechTalks 8 місяців тому

    Thanks,
    ❤ from India

  • @theMadZakuPilot
    @theMadZakuPilot 9 місяців тому

    great vid bro.

  • @tanviranjum695
    @tanviranjum695 7 місяців тому

    What’s the complexity of this sort() method

  • @dennistaranchuk4198
    @dennistaranchuk4198 9 місяців тому +1

    Life saver

  • @shieldanime6968
    @shieldanime6968 Рік тому

    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 11 місяців тому

      yep it the same as number.sort((b,a) => b - a) it is just a naming

  • @vallunacoder.wecodetogether

    hey Bro thanks for your videos, can you make some projects to understand how to use all this JavaScript methods for a website ?

  • @fighter8931
    @fighter8931 Місяць тому

    Nice

  • @AAYUSHAHUJA-gc4tu
    @AAYUSHAHUJA-gc4tu 25 днів тому

    thankyou ❤

  • @yuvrajsingh-fz7hd
    @yuvrajsingh-fz7hd 5 місяців тому

    just subscibed thanks again

  • @noah_ayyubi
    @noah_ayyubi 4 місяці тому

    u didnt explain the reason why tho

  • @kathikr9360
    @kathikr9360 11 місяців тому

    thank you

  • @yuvrajsingh-fz7hd
    @yuvrajsingh-fz7hd 5 місяців тому

    thanks man

  • @NikoZen-m7w
    @NikoZen-m7w 2 місяці тому

    Thanks...

  • @allhailalona
    @allhailalona 7 місяців тому +2

    i would think squidward is more intelligent than spongebob

  • @khansikki
    @khansikki 7 місяців тому

    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}}

  • @kathikr9360
    @kathikr9360 9 місяців тому

    thanks again

  • @Blitz61wasd
    @Blitz61wasd Рік тому +1

    Cool

  • @Drelka
    @Drelka 11 місяців тому

    thanks bro

  • @aaronsworld191
    @aaronsworld191 Місяць тому

    Bro it doesnt work

  • @tauhidxahmed
    @tauhidxahmed Рік тому

    hey bro