Тёмный

JavaScript Problem: Sorting an Array of Objects 

All Things JavaScript, LLC
Подписаться 39 тыс.
Просмотров 55 тыс.
50% 1

The sort method of JavaScript arrays allows the use of a callback function. For many sort features, this is necessary. In this tutorial we look at sorting an array of objects.
Would you like to help keep this channel going?
/ allthingsjavascript
Access to EVERY course (get 2 months free): www.skillshare...
Courses offered on Udemy at a discount:
Getting Started: www.udemy.com/...
Advanced Topics: www.udemy.com/...
Mastering Regular Expressions in JavaScript:
www.udemy.com/...
Functional Programming in JavaScript: www.udemy.com/...
NEW Mastering JavaScript Arrays: www.udemy.com/...
Tutorials referred to in this video:
Understanding Callbacks: • Understanding JavaScri...
The Callback Pattern: • JavaScript Patterns: T...
For more resources on JavaScript:
www.allthingsja...
#javascript #AllThingsJavaScriptLLC

Опубликовано:

 

15 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 73   
@soumyaranjanpradhan1325
@soumyaranjanpradhan1325 3 года назад
Sir, you explain in the core concept , I understood better by seeing your lecture ,nice explain , Sir !!
@AllThingsJavaScript
@AllThingsJavaScript 3 года назад
Glad to hear that
@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!
@AllThingsJavaScript
@AllThingsJavaScript Год назад
Glad it was helpful!
@romansad
@romansad 4 года назад
Me sirvio perfecto necesitaba ordenar un array de objetos por una propiedad numerica especifica y este video me saco del inframundo!!
@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!
@AllThingsJavaScript
@AllThingsJavaScript 2 года назад
Glad it helped!
@themimotwins7544
@themimotwins7544 Год назад
Thank you so much i was looking for this to make my global leaderboard!!!
@AllThingsJavaScript
@AllThingsJavaScript Год назад
Glad I could help!
@skolo65
@skolo65 Год назад
Super! Thank you very much. First 6 minutes and my task is done.
@AllThingsJavaScript
@AllThingsJavaScript Год назад
Glad it helped!
@michaelnorton9715
@michaelnorton9715 11 месяцев назад
Thanks this helped me solve the sort issue I had for my website project for school. :)
@AllThingsJavaScript
@AllThingsJavaScript 11 месяцев назад
Glad it helped!
@9990490677
@9990490677 2 года назад
What if we do a return a.lastName.toLowerCase().localeCompare(b.lastName.toLowerCase()) ?
@J7777788888
@J7777788888 4 года назад
thanks ! ur explanation is better than my prof : D
@hendrixsergio5474
@hendrixsergio5474 3 года назад
instablaster...
@rafaelromano4817
@rafaelromano4817 3 года назад
greath explanation, a big thanks from Brasil
@uzairkhan5112
@uzairkhan5112 Год назад
Thank you so much!!! it was getting really frustrating
@AllThingsJavaScript
@AllThingsJavaScript Год назад
Glad it helped!
@mariacamilamontealegremerc5892
@mariacamilamontealegremerc5892 2 года назад
Excellent tutorial, thank you so much!
@suvadeepdutta3461
@suvadeepdutta3461 2 года назад
Thank you ma'am , for the helpful suggestions .
@Manojshankar36
@Manojshankar36 2 года назад
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 года назад
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.
@eldersonlaborit2290
@eldersonlaborit2290 4 года назад
Save my life, thank's.
@mrz1764beatle
@mrz1764beatle 3 года назад
This is HUMONGOUS!!!
@mirkamrulahsanasif3474
@mirkamrulahsanasif3474 2 года назад
Thank You very much sir.....
@zainahmadchattha2177
@zainahmadchattha2177 3 года назад
thanks a lot for making this video. it was very helpful.
@AllThingsJavaScript
@AllThingsJavaScript 3 года назад
Glad it was helpful!
@srishtijaiswal7079
@srishtijaiswal7079 2 года назад
Helpful
@MoviesForFunLT
@MoviesForFunLT 4 года назад
Hi, could you increase this array to 1000 objects and then paginate it while keeping sorting functionallity
@AllThingsJavaScript
@AllThingsJavaScript 4 года назад
That should work. Did you have any problems with it?
@pronobdas1615
@pronobdas1615 Год назад
It was helpful ☘️
@00ofer
@00ofer 2 года назад
great video! thank you!!
@saranagarwal6298
@saranagarwal6298 Год назад
Time complexity?
@hsutheo8740
@hsutheo8740 2 года назад
That‘s Awesome. It did help!
@purplexionroblox
@purplexionroblox 2 года назад
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 года назад
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 4 года назад
Mind I ask why you .lowercase() the values? I’m assuming for a guaranteed confirmation that all the values are case sensitive?
@giovsteph
@giovsteph 4 года назад
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 4 года назад
yes, that was the reason
@Zohali
@Zohali 2 года назад
Thank you
@charujain4308
@charujain4308 2 года назад
Great explanation !!!
@antoniopavic2547
@antoniopavic2547 Год назад
good bless you
@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?
@juanhedderich
@juanhedderich Год назад
thanks you sir
@oldgames001
@oldgames001 2 года назад
Thanks Man
@BobbyBundlez
@BobbyBundlez 4 года назад
arr.sort((a,b)=>a.score>b.score?1:-1)
@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.
@subrotachandrasarker3081
@subrotachandrasarker3081 2 года назад
You are great
@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.
@HarmanSingh-oy3rr
@HarmanSingh-oy3rr 5 лет назад
great video
@tanoth1pl
@tanoth1pl 5 лет назад
Very useful, thx
@kinstar
@kinstar 2 года назад
thank you :D
@geethakrishna7434
@geethakrishna7434 3 года назад
how to do the asc,des sort at a time
@AllThingsJavaScript
@AllThingsJavaScript 3 года назад
At the same time?
@raj080288
@raj080288 4 года назад
why would you want to store objects in an array and vice versa?
@AllThingsJavaScript
@AllThingsJavaScript 4 года назад
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 года назад
@@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 4 года назад
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 года назад
@@vikrambam1184 I'm planning a tutorial to explain it.
@AllThingsJavaScript
@AllThingsJavaScript 2 года назад
Posted the tutorial now: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-IfieE2G3bfg.html
@gettingoverit__
@gettingoverit__ 11 месяцев назад
dry runnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn????????????????????????????????????????
@youthhome8450
@youthhome8450 3 года назад
great Video
Далее
JavaScript Comparator Function | Sorting Explained!
12:21
# funny#daily#vlog#family#prank
00:12
Просмотров 7 млн
Understanding JavaScript Callbacks
15:39
Просмотров 115 тыс.
5 JavaScript Concepts You HAVE TO KNOW
9:38
Просмотров 1,4 млн
Using a Double Sort on an Array of Objects
12:28
Просмотров 8 тыс.
Easy Ways to Loop Over Objects in JavaScript
11:21
Просмотров 10 тыс.
Generics: The most intimidating TypeScript feature
18:19
The Magic of the reduce Array Method
15:24
Просмотров 22 тыс.
Learn JavaScript Array Reduce In 10 Minutes
10:22
Просмотров 201 тыс.