sort Array Method | JavaScript Tutorial

Поделиться
HTML-код
  • Опубликовано: 2 дек 2024

Комментарии • 157

  • @FlorinPop
    @FlorinPop  4 года назад +10

    Check out the other videos in the JavaScript Array Methods series: ruclips.net/p/PLgBH1CvjOA62PBFIDq55-S6Beivje30A2

    • @iamibayat
      @iamibayat 4 года назад

      how do we sort the name ? You have shown the price only.

  • @RafaelSales55
    @RafaelSales55 3 года назад +13

    Thank you a lot, I was stuck for like 7 hours trying to swap every array in the list. This method is so much simple and cool

  • @clarinetisfying
    @clarinetisfying 2 года назад +9

    simple, short, and very clean explanation I was looking for! thanks!

  • @AG-qp7gn
    @AG-qp7gn 3 года назад +1

    Man, I thank god that I found you, thanks for tutorial, you saved me a lot of time. Love you man !

  • @muadzrosli
    @muadzrosli 4 года назад +12

    Hi @florin pop. I am no clear about the compare function you put inside sort method. How the comparing is done to sort all numbers because the compare function has a and b which are two parameters? Does sort method take number at index 0 and compare to at index 1, etc?

    • @FlorinPop
      @FlorinPop  4 года назад +10

      Yes. It takes all the pairs starting with 0 and 1, then 1 and 2 and so on until it’s sorted. It might even go back on testing 0 and 1 again, but I’m not sure 100% which algorithm is used in JS for sorting.

    • @muadzrosli
      @muadzrosli 4 года назад

      @@FlorinPop thanks. Anyway, I would also think that the method keeps checking until all elements are sorted correctly.

    • @FlorinPop
      @FlorinPop  4 года назад +1

      Yes

    • @prizonier2
      @prizonier2 Год назад

      @@FlorinPop Wouldn't that take a very long time if you'd have many numbers ?

  • @saravanan.r5079
    @saravanan.r5079 3 года назад +14

    Can you please explain why a comes first when it returns positive value...that's the confusion I have☹️

    • @nitishkumargarikapati7767
      @nitishkumargarikapati7767 2 года назад +1

      same doubt

    • @Ii-fo8pq
      @Ii-fo8pq 2 года назад +1

      @@nitishkumargarikapati7767
      > 0 sort a after b
      < 0 sort a before b
      === 0 keep original order of a and b
      Now subtract the values in arrays considering the first value as 'a' next value as 'b' one by one, if the subtraction returns positive integer i.e. >0 it'll place the value 'b' first in the array or if it returns

    • @sauldownbadman876
      @sauldownbadman876 Год назад

      @@Ii-fo8pq still doesn't make any sense. Does a and b change? Like if a goes before b then does b become the new a?

    • @uchennachukwuba
      @uchennachukwuba Год назад +2

      ​@@sauldownbadman876 This is just the convention that the sort() method follows in JavaScript, and it's important to keep in mind when writing comparison functions for sorting arrays of numbers or other types of elements.

    • @tramarthomas6105
      @tramarthomas6105 Год назад

      ​@@Ii-fo8pq thanks, I had no clue what he meant by 'comes first'

  • @A_Saban
    @A_Saban 3 года назад

    dude i have no words like what... you have no idea the wierd explanations I saw for this and you made it so easy. thank you

  • @tammybatubo9525
    @tammybatubo9525 4 года назад +3

    This is amazing florin. I follow you on twitter but this is the first time I came here. I finally understand this function

    • @FlorinPop
      @FlorinPop  4 года назад

      Happy to hear that 😃

  • @negativepunk9638
    @negativepunk9638 3 года назад +1

    if(array.length > 2) a = current; b = next;
    .sort() method is looping over the array
    I hope you gets it, this is the simplest way I could explain it

    • @negativepunk9638
      @negativepunk9638 3 года назад

      this is for the people confused what a and b are for

  • @kevinbegin3049
    @kevinbegin3049 3 года назад +5

    Super helpful tutorial. I was trying to sort arrays of numbers and couldn't figure out why it wasn't working as expected and what exactly happened when you told sort to use 'a-b'.
    You gave me all the info I needed. Thank you!

    • @FlorinPop
      @FlorinPop  3 года назад +2

      Glad it helped ☺️

  • @haruNL
    @haruNL 4 года назад +4

    great video and a good explanation for self-learner. too much appreciated.

  • @alanwalker5845
    @alanwalker5845 2 года назад

    Finally understood. Read shit loads of blogs but nowhere was that greater and smaller condition mentioned properly, all of them just stated condition.

  • @moududahammed880
    @moududahammed880 Год назад

    Explanation was short and sweet . Frankly speaking , I got the main point .

  • @Debjit_Adhikari
    @Debjit_Adhikari 10 месяцев назад

    Thank you so much. I was stuck on this sort() method. You made things easy.

  • @veekay2254
    @veekay2254 2 года назад

    Natural teacher!

  • @ayMillmusic
    @ayMillmusic Год назад

    First part of this series that I'm completely lost 😂

  • @ivangelinelol
    @ivangelinelol 2 года назад

    Thank you for showing descening and ascending fantastic video

  • @jitujahagirdar613
    @jitujahagirdar613 2 года назад +1

    How to sort mix array where it has number, letter and emoji symbol .?
    [“q”, 9,5,🤪, 🤨1,6,3,8,😊 , “a” “g”]

  • @gideonilm4983
    @gideonilm4983 2 года назад

    I was stuck in this thing. This really cleared things up.

  • @joaopedroalves6777
    @joaopedroalves6777 5 месяцев назад

    javascript is prety cool. Also, thanks for the explanation, very clear and concise.

  • @puneetwasan771
    @puneetwasan771 3 года назад +3

    How would it pick up the values of 'a' and 'b' from the array?

    • @christosbinos8467
      @christosbinos8467 2 года назад +6

      "a" and "b" are variables that stand for any 2 given numbers in the array.
      The .sort() method runs through every element of the array. When you pass a function which contains a - b in it, you're telling the sort method to go over the entire array and subtract index[0] from index[1], then index[2] - index[3] etc.
      Where the confusion exists for many is that the .sort method checks whether the output of that subtraction is less than 0 or not (we don't write that code, it's included in .sort, hence what makes it a method.)
      The sort method knows which number is bigger because if you subtract 3 - 6 (a - b) you will get -3. That means that b is bigger than a. In order for the result to be positive it would have to be 6 - 3 (a - b) which equals 3. Therefore the number to the right is smaller than the number to the left and is therefore moved one position closer to the beginning.
      The .sort has an internal algorithm which when passed (a-b) subtracts all the elements of the array with each other to determine which number is larger. It determines this by seeing whether the output of each subtraction it performs is positive or negative. If the result is negative then b must be bigger than a, therefore it must move closer to the beginning.

  • @datawrangler8565
    @datawrangler8565 3 года назад

    how did you get the output on the right hand side of vs code? have tried everything but i can only ues a browser to print out my console. Have tried installing all the extensions on vs code but it doesn't work

  • @darkopesevski9829
    @darkopesevski9829 Год назад

    Thank you so much for the clear explanation!

  • @itsamazing22
    @itsamazing22 3 года назад

    Now, I understand what sort is used for arrays. Thanks!

  • @smridhisingh111
    @smridhisingh111 Год назад

    Thank you for making the video it helped me a lot while i was confused.

  • @hamzailyas9053
    @hamzailyas9053 3 года назад

    Hello Florin ! My Question is How i Find Third Largest Value Using set, sort and reverse in JavaScript ?

  • @RafaelMendes-yw2jm
    @RafaelMendes-yw2jm 2 года назад

    Yeah... if we need to sort between an object parameter that can have strings or integers, what suggestion could you recommend here?

  • @Anonymous-ns2yf
    @Anonymous-ns2yf 2 года назад

    thank you so much . I was stuck there

  • @naimurrahman9979
    @naimurrahman9979 3 года назад

    I am grateful to you brother for easy explanation.. Thanks very much

  • @hotcoffeehero
    @hotcoffeehero Год назад

    Great explanation.

  • @simonesalvalaggio
    @simonesalvalaggio 2 года назад

    How do you use the terminal as a preview screen?

  • @EnzoAuditore
    @EnzoAuditore 2 года назад

    Thank you mate!

  • @andrerodrigues3322
    @andrerodrigues3322 4 года назад +3

    Awesome explanation 👏🏼❤️ Thank you so much! Keep going :)

  • @benaliissameddine5697
    @benaliissameddine5697 Год назад

    Thank you so much , this is very clear explanation!

  • @Philosophy.and.Tostitos
    @Philosophy.and.Tostitos 3 года назад +1

    Thank you! just exactly what I was looking for :)

  • @scorpawe
    @scorpawe 2 года назад

    very helpful. you have my thanks

  • @Mariamasmagnet
    @Mariamasmagnet Год назад

    thanks a lot for this video!!!!

  • @EazyE206
    @EazyE206 2 года назад

    Made it so clear, thank you!

  • @mrgamerzyt3945
    @mrgamerzyt3945 11 месяцев назад

    is it fixed now ot smth? because the .sort() is working for numbers now

  • @SadmanRyanRiad
    @SadmanRyanRiad Год назад

    great explanation.

  • @ThrashDawg
    @ThrashDawg Год назад

    Thanks, I was unsure about how things "came first" until I came across this video.

  • @ultronsam1214
    @ultronsam1214 2 года назад

    In your example you said a is 10 and b is 5. I thought those letters could be represented as; current, previous. Where 5 is current and 10 is previous, and that'd be -5. Hence, 5 would be sorted first. Anyways, I appreciate the video, it helped me to understand some things. God bless! Correct me if I'm wrong tho.

    • @random__handle
      @random__handle Год назад

      Actually a is the current and b is the next, or if you will a is the previous and b is the current. In any case it's in the same order as you write "(a, b)"!

  • @nishatislam3495
    @nishatislam3495 2 года назад

    best explanation, thanks, sir

  • @second_inning_development
    @second_inning_development 10 месяцев назад

    thankyou, you gain a subscriber

  • @AigleLee18
    @AigleLee18 10 месяцев назад

    Thank you for the great explanation

    • @FlorinPop
      @FlorinPop  10 месяцев назад

      Glad it was helpful!

  • @miedsekadse7136
    @miedsekadse7136 2 года назад

    Great video, thanks.

  • @Sch0LarY
    @Sch0LarY Год назад

    clean and to the point, thank you.

  • @TheFryPo
    @TheFryPo 3 года назад

    Great tutorial, thank you!

  • @Agustin-jo8mv
    @Agustin-jo8mv 2 года назад

    Damn I wish I know 10% of Florins JS knowledge! I saw your 10 projects in 10 hours so proud of you. haha.

  • @FolakemiOgunseiju
    @FolakemiOgunseiju Год назад

    should it be minus or to? Because it confused me

  • @AbhishekSingh-rs6tx
    @AbhishekSingh-rs6tx 2 года назад

    well explained, thank you sir :)

  • @suprabhatkumar5478
    @suprabhatkumar5478 Год назад

    Awesome. Thank you brother

  • @محمدالنجار-ت6ب
    @محمدالنجار-ت6ب 2 года назад

    thank you my friend

  • @buildmediapro
    @buildmediapro Год назад

    Nice, Thank you so much

  • @procopio3274
    @procopio3274 3 года назад

    Thank you for this.

  • @denyslinetskyi
    @denyslinetskyi 2 года назад

    Thank you so much!)

  • @jackfrost8969
    @jackfrost8969 4 года назад

    Excellent. Exactly what I was looking for.

    • @jackfrost8969
      @jackfrost8969 3 года назад

      @@lycan2494 my pp is from db super though

  • @kolbecrypto2976
    @kolbecrypto2976 2 года назад

    Tnx florin was usefull

  • @jitendrasoni191
    @jitendrasoni191 Год назад

    Gajab Sirg

  • @bama2619
    @bama2619 11 месяцев назад

    fun tricks in sorting. Thank you

    • @FlorinPop
      @FlorinPop  11 месяцев назад

      You're welcome! 😃

  • @luciano7093
    @luciano7093 3 года назад

    thank Bro
    good explanation

  • @gyanology3356
    @gyanology3356 3 года назад

    Nice video sir. It helped me very much to understand the concept. Thankyou very much for making such amazing content like this.

  • @AmberRathour366
    @AmberRathour366 2 года назад

    Well explained

  • @goldroger1368
    @goldroger1368 5 месяцев назад

    how do i turn my visual code like that? it automatically see the output when need to type node js command?

    • @FlorinPop
      @FlorinPop  5 месяцев назад

      Look into nodemon

  • @Emma-eb5zb
    @Emma-eb5zb 2 года назад

    How can I sort divs by their id number?

  • @libradev9786
    @libradev9786 2 года назад

    if I have -> const products = [ {name: 'laptop', weight: {n_weight: 2, g_weight: 2.3}}, {name: 'desktop', weight: {n_weight: 5, g_weight: 5.6}} ]
    how to sort n_weight or g_weight

  • @RICK_OP_
    @RICK_OP_ Год назад

    Thanks a lot bro

  • @simple8810
    @simple8810 3 года назад

    very well explained m ur new subscriber

  • @rituparnasarkar5032
    @rituparnasarkar5032 Год назад

    Thank you😊

  • @francodosha
    @francodosha Год назад

    I love this

  • @digiexploration530
    @digiexploration530 3 года назад

    great stuff, reall easy to understand. I am a newcomer. Compare that with MDN page on sorting lol

  • @ЯнисДрейманис-ъ4щ
    @ЯнисДрейманис-ъ4щ 2 года назад

    thank you!

  • @highlycurious
    @highlycurious 10 месяцев назад

    great help thankyou sir

    • @FlorinPop
      @FlorinPop  10 месяцев назад

      You are welcome!

  • @kubilayistikam6382
    @kubilayistikam6382 3 года назад

    keep good work up bro

  • @prajulsahu9838
    @prajulsahu9838 2 года назад

    Hey, what to do if I have a symbol infront of numbers in number array, like, [$74, $18, $10, $2, $84, $24, $105]?

    • @FlorinPop
      @FlorinPop  2 года назад

      Those are strings. In the sort function you’ll have to convert them to numbers and compare it that way

  • @random2059
    @random2059 Год назад

    .sort () use bubble sort internally ??????

  • @vivekbhatt3932
    @vivekbhatt3932 Год назад

    Got it bro! 👍

  • @scottcoleman5438
    @scottcoleman5438 2 года назад

    Hello, new to JS, and I put this code in without the function and it sorted the numbers automatically. Is this a new function added since this video came out?
    const numbers3 = [12, 87, 23, 42, 33, 99, 43];
    numbers3.sort();
    console.log(numbers3);

  • @sallylauper8222
    @sallylauper8222 3 года назад

    It's deceptively simple!

  • @sagarkamble7810
    @sagarkamble7810 2 года назад

    Thanks

  • @cooljohnny9402
    @cooljohnny9402 4 года назад +2

    man, love this format. Damn! :D Keep'em going. be careful of le burnout haha

  • @Aakash-yh8ou
    @Aakash-yh8ou Год назад

    Thnx a lot sir

  • @manueduardo2002
    @manueduardo2002 2 года назад

    oh man, I don't speak english but your video is so good that I could understand

  • @ilya_123__
    @ilya_123__ Год назад

    thank you

  • @saabbaaz9369
    @saabbaaz9369 4 года назад

    What editor are you using here? thanks

  • @AccordingtoWarren
    @AccordingtoWarren 2 года назад

    Does anyone know what the run time of this sort() method is?

    • @FlorinPop
      @FlorinPop  2 года назад

      twitter.com/florinpop1705/status/1594600268385357824?s=46&t=iVn_uTF2LYs7nB1sYBhSxQ

  • @zafarhussain8273
    @zafarhussain8273 4 года назад +2

    what is a and b here . i don't understand it

    • @obscurity87
      @obscurity87 4 года назад +2

      yea i wish he dug a little deeper here a little confused with the logic here as well

    • @MaxProgramming
      @MaxProgramming 4 года назад

      a and b here are the 1st number and 2nd number respectively. Then on the second iteration, it's 2nd and 3rd and so on

    • @zafarhussain8273
      @zafarhussain8273 4 года назад +1

      @@MaxProgramming ohh got it thank alot

    • @MaxProgramming
      @MaxProgramming 4 года назад +1

      @@zafarhussain8273 Well, maybe I was wrong. It can also be a = 1st, b = 2nd - a = 3rd, b = 4th - a = 5th, b = 1st. If there are 5 items in an array

    • @MaxProgramming
      @MaxProgramming 3 года назад

      @@lycan2494 Thank you. I get a littlie confused 😅

  • @ShaunDVine
    @ShaunDVine 3 года назад

    Thank you. I had been struggling to sort years in a filter in ascending order. This did the trick.

  • @fethisert5527
    @fethisert5527 9 месяцев назад

    Thank you sir

  • @ritikdwivedi12
    @ritikdwivedi12 4 года назад

    Thanks Pop

  • @iamibayat
    @iamibayat 4 года назад

    how do we sort the name ? In video the price is shown only.

    • @jritzeku
      @jritzeku 4 года назад +1

      products.sort((a, b) => a.name.localeCompare(b.name)); //localeCompare( ) "Determines whether two strings are equivalent in the current locale."

    • @henokbekele7523
      @henokbekele7523 4 года назад

      @@jritzeku can you do the same thing with out using .localecompare()

  • @nayyarabbas4482
    @nayyarabbas4482 Год назад

    can someone tell how is his terminal automatically updating with console.log("")??

  • @nadeemakramansari1331
    @nadeemakramansari1331 9 месяцев назад

    In a nutshell can I say, a - b -> accending order while b - a -> will be descending order ?

  • @adebusoyeolajideadewumifun9122

    how do i return a name in an array?

    • @FlorinPop
      @FlorinPop  Год назад

      I need more details in order to help you

  • @errorsolver4994
    @errorsolver4994 2 года назад

    awesome

  • @shalauddinahmedshipon6387
    @shalauddinahmedshipon6387 8 месяцев назад

    Thanks bro

  • @georgegeorgio70
    @georgegeorgio70 3 года назад

    const products = [
    {
    name: 'laptop',
    price: 1000
    },
    {
    name: 'desktop',
    price: 1500
    },
    {
    name: 'phone',
    price: 500
    }
    ];

  • @rqd3
    @rqd3 3 года назад +2

    you are literally scratching the surface and letting us pressume that the sort() function does all the magic by itself.

    • @negativepunk9638
      @negativepunk9638 3 года назад

      not neccesarily, this is as clear and straightforward as it could get. Do you want him to teach you from variable assigning? If you cant catch up with him, you need to go back reviewing what you have learned so far. This is like level 2 JavaScript out of 10 level.

    • @b1upp
      @b1upp 3 года назад

      You can also read about it in MDN. Personally I find it a quite straight forward video.

  • @Smazzish
    @Smazzish 2 года назад

    Nice

  • @theai2020
    @theai2020 2 года назад

    The number short only shorts the first number