JavaScript Array sort() vs toSorted() (new in ES 2023)

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ก.ย. 2024

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

  • @juancarlosserradaquiva3705
    @juancarlosserradaquiva3705 18 วันที่ผ่านมา

    Hello, thanks for your videos, I would like to ask you a question, how can I sort an array of objects using toSorted() ? (I am using Angular 18)

    • @deborah_kurata
      @deborah_kurata  18 วันที่ผ่านมา

      I put together an example here: stackblitz.com/~/edit/tosorted-array-deborahk
      Hope this helps.

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

    Or we can simply do ages.slice().sort((a, b) => a- b). Slice without params make a copy of the array.

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

      Yes. But they aren't implemented the same. You can see the implementation specification for `toSorted` here: tc39.es/ecma262/#sec-array.prototype.tosorted
      You can compare that to `slice` here: tc39.es/ecma262/#sec-array.prototype.slice

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

    Thank you for teaching this, really informative and helpful

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

      So great to hear! Thank you so much!

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

    Thanks Professor

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

    So it's a more prettier and concise way of doing :
    const sortedAges = ages.slice().sort((a, b)=> a - b), am I correct?

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

      Definitely more concise, more obvious and more clear. 😊

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

    I found your tutorial to be very informative. Thank you for sharing it.

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

      Glad it was helpful! Thank you!

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

    Thanks

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

      Thanks for watching!

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

    Thank you!

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

      Thank you for watching!

  • @Eslam-ig2gf
    @Eslam-ig2gf 8 หลายเดือนก่อน

    You mentioned in your last two videos that mutating the original array causes problems with applications that support immutable patterns, but I can't imagine how this occurs?
    Please can you explain it?

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

      I hope I said that it may cause problems with application that support "immutable patterns". Did I accidentally say "mutable patterns" somewhere?

    • @Eslam-ig2gf
      @Eslam-ig2gf 8 หลายเดือนก่อน

      I am sorry, I have edited it.

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

      Immutable patterns expect that a variable value is replaced with a new value, not modified "in place".
      Let's use an array as an example. Mutating the original array does *not* change the variable value. The variable is still set to the same memory address. So if the immutable pattern is looking for a change to the variable, it won't see that the variable changed.
      If you instead create a new array at a new memory address, and set the variable to that new array, any immutable pattern will see that the variable has changed.
      You can try out the code I linked in the video notes of the "Immutability video" to see that the setter is *not* called if an array is mutated in place.
      Did that clarify?

    • @Eslam-ig2gf
      @Eslam-ig2gf 8 หลายเดือนก่อน

      Yeah, but what makes me confused is: when the program get the value of a variable it tracks its address to get its value, ex: let arr = [1,2,3], its address arr:c4 , c4:[1,2,3], so the program will track these addresses to get the value of the variable each time unless it stores the actual value in another place so it will not recognize the change in this case.
      That is what I think to get the value of the variable, or do I have misunderstanding?
      Or by other words I don't know how immutable patterns work.

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

      ​@@Eslam-ig2gfyou should check out reference topic in javascript, how javascript recognize whether an element has changed or not