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!
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
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.
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!
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.
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"}
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.
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 ));
Sir, you explain in the core concept , I understood better by seeing your lecture ,nice explain , Sir !!
Glad to hear that
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!
Glad it was helpful!
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!
Glad it helped!
Thank you so much i was looking for this to make my global leaderboard!!!
Glad I could help!
Super! Thank you very much. First 6 minutes and my task is done.
Glad it helped!
Thanks this helped me solve the sort issue I had for my website project for school. :)
Glad it helped!
Me sirvio perfecto necesitaba ordenar un array de objetos por una propiedad numerica especifica y este video me saco del inframundo!!
Thank you so much!!! it was getting really frustrating
Glad it helped!
thanks ! ur explanation is better than my prof : D
instablaster...
greath explanation, a big thanks from Brasil
Excellent tutorial, thank you so much!
What if we do a return a.lastName.toLowerCase().localeCompare(b.lastName.toLowerCase()) ?
That‘s Awesome. It did help!
great video! thank you!!
Thank you ma'am , for the helpful suggestions .
thanks a lot for making this video. it was very helpful.
Glad it was helpful!
Save my life, thank's.
This is HUMONGOUS!!!
It was helpful ☘️
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
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.
@@AllThingsJavaScript
Thank you so much for the reply.
Thank You very much sir.....
Great explanation !!!
good bless you
Hi, could you increase this array to 1000 objects and then paginate it while keeping sorting functionallity
That should work. Did you have any problems with it?
Helpful
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!
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.
@@AllThingsJavaScript Thanks for replying to my comment!
Mind I ask why you .lowercase() the values? I’m assuming for a guaranteed confirmation that all the values are case sensitive?
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.
yes, that was the reason
Thank you
Thanks Man
You are great
great video
thanks you sir
how can you compare two string in the first example ?? i didnt get it !!
Could you be more specific about which part you have a question?
Very useful, thx
thank you :D
arr.sort((a,b)=>a.score>b.score?1:-1)
Time complexity?
Can we use else if?
Sure. No problem. An if statement with a return ends the code at that point. That is the reason I used that.
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"}
Create an array of values using Object.values and then sort the array.
how to do the asc,des sort at a time
At the same time?
why would you want to store objects in an array and vice versa?
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.
@@AllThingsJavaScript so anything which may contain a list of things would be easily stored in an array?
@@raj080288 Yes, definitely. Lists of anything. Arrays have so many more methods in order to work with an manage the items.
@@AllThingsJavaScript thank you for clearing that up and also for the prompt reply. Keep up the great videos!
thanks
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'
},
]
},
]
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 ));
@@AllThingsJavaScript yeaah , this work completly Fine thanks for this can you explain this..
@@vikrambam1184 I'm planning a tutorial to explain it.
Posted the tutorial now: ua-cam.com/video/IfieE2G3bfg/v-deo.html
dry runnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn????????????????????????????????????????
great Video