JavaScript Problem: Sorting an Array of Objects

Поділитися
Вставка
  • Опубліковано 1 лис 2024

КОМЕНТАРІ • 73

  • @soumyaranjanpradhan1325
    @soumyaranjanpradhan1325 3 роки тому +10

    Sir, you explain in the core concept , I understood better by seeing your lecture ,nice explain , Sir !!

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

    Just what I needed to understand how to sort my list of students. Your explanation is super clear and easy to follow. Thanks so much!

  • @mocoebabymom
    @mocoebabymom 2 роки тому

    im pretty new to JS and have been trying to make this sorting function for a whole day then i watched your video and my problem is solved. very much thanks!

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

    Thank you so much i was looking for this to make my global leaderboard!!!

  • @skolo65
    @skolo65 2 роки тому

    Super! Thank you very much. First 6 minutes and my task is done.

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

    Thanks this helped me solve the sort issue I had for my website project for school. :)

  • @romansad
    @romansad 5 років тому +2

    Me sirvio perfecto necesitaba ordenar un array de objetos por una propiedad numerica especifica y este video me saco del inframundo!!

  • @uzairkhan5112
    @uzairkhan5112 2 роки тому

    Thank you so much!!! it was getting really frustrating

  • @J7777788888
    @J7777788888 4 роки тому +2

    thanks ! ur explanation is better than my prof : D

  • @rafaelromano4817
    @rafaelromano4817 4 роки тому +2

    greath explanation, a big thanks from Brasil

  • @mariacamilamontealegremerc5892
    @mariacamilamontealegremerc5892 2 роки тому +1

    Excellent tutorial, thank you so much!

  • @9990490677
    @9990490677 2 роки тому +1

    What if we do a return a.lastName.toLowerCase().localeCompare(b.lastName.toLowerCase()) ?

  • @hsutheo8740
    @hsutheo8740 2 роки тому

    That‘s Awesome. It did help!

  • @00ofer
    @00ofer 2 роки тому +1

    great video! thank you!!

  • @suvadeepdutta3461
    @suvadeepdutta3461 2 роки тому

    Thank you ma'am , for the helpful suggestions .

  • @zainahmadchattha2177
    @zainahmadchattha2177 3 роки тому

    thanks a lot for making this video. it was very helpful.

  • @eldersonlaborit2290
    @eldersonlaborit2290 4 роки тому +2

    Save my life, thank's.

  • @mrz1764beatle
    @mrz1764beatle 4 роки тому +1

    This is HUMONGOUS!!!

  • @pronobdas1615
    @pronobdas1615 2 роки тому

    It was helpful ☘️

  • @Manojshankar36
    @Manojshankar36 2 роки тому +1

    Nice video.
    I have a quick query,
    How can we do sorting through different values( like Pending, Approve, cancel ) instead of ascending or descending order.
    Something like (not ascending or descending order):
    1. Approve , Cancel, Pending
    2. Cancel, Pending, Approve
    3. Pending, Approve, Cancel
    Please let me know your thoughts.
    Thanks and appreciate it

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 роки тому +2

      So it really depends upon what values are stored for fields like that: approve, cancel, pending. Then you would write a function that goes through each value and orders based on that. Maybe approve is just true or false, which would be easy. Decide which you want first and then return > 0 for that item. Then if you want sorted by cancel next, send the results to a new function that would sort based on that and so on. You would do it one at a time; different function for each one.

    • @Manojshankar36
      @Manojshankar36 2 роки тому

      @@AllThingsJavaScript
      Thank you so much for the reply.

  • @mirkamrulahsanasif3474
    @mirkamrulahsanasif3474 2 роки тому

    Thank You very much sir.....

  • @charujain4308
    @charujain4308 3 роки тому

    Great explanation !!!

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

    good bless you

  • @MoviesForFunLT
    @MoviesForFunLT 5 років тому +1

    Hi, could you increase this array to 1000 objects and then paginate it while keeping sorting functionallity

  • @srishtijaiswal7079
    @srishtijaiswal7079 2 роки тому +1

    Helpful

  • @purplexionroblox
    @purplexionroblox 2 роки тому +1

    Nice tutorial, but I'm still wondering why we have to return the negative number, the positive number and the zero. If you could please reply to my comment as it would really help me out. Thanks!

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 роки тому +2

      That is just how the sort function was defined. The negative number, positive number, or 0 indicates how the sorting will occur. Sort compares 2 values at a time and the number you return determines the order of those values.

    • @purplexionroblox
      @purplexionroblox 2 роки тому

      @@AllThingsJavaScript Thanks for replying to my comment!

  • @erving_lanc324
    @erving_lanc324 5 років тому

    Mind I ask why you .lowercase() the values? I’m assuming for a guaranteed confirmation that all the values are case sensitive?

    • @giovsteph
      @giovsteph 5 років тому +2

      Yes, the sort function is case sensitive and if you don't convert to lower case, it will consider the upper case letters and place them first.

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 років тому +1

      yes, that was the reason

  • @Zohali
    @Zohali 2 роки тому +1

    Thank you

  • @oldgames001
    @oldgames001 2 роки тому

    Thanks Man

  • @subrotachandrasarker3081
    @subrotachandrasarker3081 2 роки тому

    You are great

  • @HarmanSingh-oy3rr
    @HarmanSingh-oy3rr 5 років тому +1

    great video

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

    thanks you sir

  • @Flame-on-f2p
    @Flame-on-f2p 4 роки тому

    how can you compare two string in the first example ?? i didnt get it !!

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 роки тому

      Could you be more specific about which part you have a question?

  • @tanoth1pl
    @tanoth1pl 5 років тому

    Very useful, thx

  • @kinstar
    @kinstar 2 роки тому

    thank you :D

  • @BobbyBundlez
    @BobbyBundlez 4 роки тому +1

    arr.sort((a,b)=>a.score>b.score?1:-1)

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

    Time complexity?

  • @nicholassingh138
    @nicholassingh138 2 роки тому

    Can we use else if?

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 роки тому

      Sure. No problem. An if statement with a return ends the code at that point. That is the reason I used that.

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

    I need sort this object by values. Ex: comps = { 0: "NameComp B", 1:"NameComp A", 2:"NameComp F"}
    I need this result {1:"NameComp A", 0:"NameComp B", 2:"NameComp F"}

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

      Create an array of values using Object.values and then sort the array.

  • @geethakrishna7434
    @geethakrishna7434 3 роки тому

    how to do the asc,des sort at a time

  • @raj080288
    @raj080288 4 роки тому

    why would you want to store objects in an array and vice versa?

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 роки тому +1

      Here is an example I have used. I have objects that represent questions for an online course. I store them in an array because they are much easier to manage and work with.

    • @raj080288
      @raj080288 4 роки тому

      @@AllThingsJavaScript so anything which may contain a list of things would be easily stored in an array?

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 роки тому +1

      @@raj080288 Yes, definitely. Lists of anything. Arrays have so many more methods in order to work with an manage the items.

    • @raj080288
      @raj080288 4 роки тому

      @@AllThingsJavaScript thank you for clearing that up and also for the prompt reply. Keep up the great videos!

  • @raymondgarcia5336
    @raymondgarcia5336 5 років тому

    thanks

  • @vikrambam1184
    @vikrambam1184 2 роки тому

    can anyone solve this :
    Write a function that accepts an array of objects about book reading progress (example below) and returns an array of the books grouped by status and sorted in ascending order by name.
    Example:
    Input:
    [
    {
    id: 1,
    status: 'completed',
    name: 'The Lord of the Rings'
    },
    {
    id: 2,
    status: 'in-progress',
    name: 'Lord of the Flies'
    },
    {
    id: 3,
    status: 'not-started',
    name: 'Dune'
    },
    {
    id: 4,
    status: 'not-started',
    name: 'American Gods'
    },
    {
    id: 5,
    status: 'completed',
    name: 'Ender\'s Game'
    },
    {
    id: 6,
    status: 'in-progress',
    name: 'Brave New World'
    },
    {
    id: 7,
    status: 'completed',
    name: '1984'
    },
    ]
    Output:
    [
    {
    status: 'completed',
    books: [
    {
    id: 7,
    name: '1984'
    },
    {
    id: 5,
    name: 'Ender\'s Game'
    },
    {
    id: 1,
    name: 'The Lord of the Rings'
    },
    ]
    },
    {
    status: 'in-progress',
    books: [
    {
    id: 6,
    name: 'Brave New World'
    },
    {
    id: 2,
    name: 'Lord of the Flies'
    }
    ]
    },
    {
    status: 'not-started',
    books: [
    {
    id: 4,
    name: 'American Gods'
    },
    {
    id: 3,
    name: 'Dune'
    },
    ]
    },
    ]

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 роки тому

      This should work. I may do a tutorial on it. I named the input array start.
      let final = start.reduce((acc,obj) => {
      switch (obj.status) {
      case 'completed':
      acc[0].books = [...acc[0].books, {id: obj.id,name: obj.name}];
      return acc;
      break;
      case 'in-progress':
      acc[1].books = [...acc[1].books, {id: obj.id,name: obj.name}];
      return acc;
      break;
      case 'not-started':
      acc[2].books = [...acc[2].books, {id: obj.id,name: obj.name}];
      return acc;
      break;
      }
      },[{status: 'completed',books:[]}, {status: 'in-progress',books:[]}, {status: 'not-started',books:[]}]);
      final.map(obj => obj.books.sort((bk1, bk2) => bk1.name.toString().toLowerCase() < bk2.name.toString().toLowerCase() ? -1 : 1 ));

    • @vikrambam1184
      @vikrambam1184 2 роки тому

      @@AllThingsJavaScript yeaah , this work completly Fine thanks for this can you explain this..

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 роки тому +1

      @@vikrambam1184 I'm planning a tutorial to explain it.

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 роки тому

      Posted the tutorial now: ua-cam.com/video/IfieE2G3bfg/v-deo.html

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

    dry runnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn????????????????????????????????????????

  • @youthhome8450
    @youthhome8450 3 роки тому

    great Video