Learn JavaScript SORTING in 6 minutes! 🗃

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2023
  • // 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

ความคิดเห็น • 35

  • @BroCodez
    @BroCodez  8 หลายเดือนก่อน +8

    // 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 5 หลายเดือนก่อน +8

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

  • @JadeWays
    @JadeWays 2 หลายเดือนก่อน

    Thank You! you make it easy to understand.

  • @mr.joecool
    @mr.joecool 5 หลายเดือนก่อน +1

    very cool, thanks for sharing!

  • @latostadaa
    @latostadaa 28 วันที่ผ่านมา

    Thank you, you made this easy to understand

  • @oshione7873
    @oshione7873 3 หลายเดือนก่อน +1

    This guy is such a clutch

  • @monayzah
    @monayzah 5 หลายเดือนก่อน +1

    Thank you!

  • @jamshedmorio1045
    @jamshedmorio1045 2 หลายเดือนก่อน

    Thanks brother this video is very easy to understand

  • @theMadZakuPilot
    @theMadZakuPilot 3 หลายเดือนก่อน

    great vid bro.

  • @malablondi77
    @malablondi77 2 หลายเดือนก่อน

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

  • @vallunacoder.wecodetogether
    @vallunacoder.wecodetogether 6 หลายเดือนก่อน

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

  • @kathikr9360
    @kathikr9360 5 หลายเดือนก่อน

    thank you

  • @JamieDawsonCodes
    @JamieDawsonCodes 2 หลายเดือนก่อน

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

    • @raniv5132
      @raniv5132 หลายเดือนก่อน

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

    • @JamieDawsonCodes
      @JamieDawsonCodes หลายเดือนก่อน +2

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

  • @dennistaranchuk4198
    @dennistaranchuk4198 3 หลายเดือนก่อน +1

    Life saver

  • @AnkitTechTalks
    @AnkitTechTalks 2 หลายเดือนก่อน

    Thanks,
    ❤ from India

  • @kathikr9360
    @kathikr9360 3 หลายเดือนก่อน

    thanks again

  • @tanviranjum695
    @tanviranjum695 หลายเดือนก่อน

    What’s the complexity of this sort() method

  • @Drelka
    @Drelka 5 หลายเดือนก่อน

    thanks bro

  • @hunin27
    @hunin27 8 หลายเดือนก่อน +8

    are these old videos or are they new?

    • @BroCodez
      @BroCodez  8 หลายเดือนก่อน +4

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

    • @hunin27
      @hunin27 8 หลายเดือนก่อน

      ok ok. thanks!@@BroCodez

  • @shieldanime6968
    @shieldanime6968 6 หลายเดือนก่อน

    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 5 หลายเดือนก่อน

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

  • @LearnwithRandomYoutuber
    @LearnwithRandomYoutuber 4 หลายเดือนก่อน +1

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

    • @ArconicTower
      @ArconicTower 4 หลายเดือนก่อน +2

      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 3 หลายเดือนก่อน

      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

  • @user-mk2md9pu5m
    @user-mk2md9pu5m 8 หลายเดือนก่อน +1

    Cool

  • @allhailalona
    @allhailalona หลายเดือนก่อน

    i would think squidward is more intelligent than spongebob

  • @khansikki
    @khansikki หลายเดือนก่อน

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

  • @tauhidxahmed
    @tauhidxahmed 6 หลายเดือนก่อน

    hey bro