Do comparison on Javascript object properties and return the rating property | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Do comparison on Javascript object properties and return the rating property

I am doing a simple javascript project where i got an array of objects... and each object has its own properties which include (title)string, rating(int), I have code here that does the comparisons but prints the whole array elements in an ascending manner, Is there a way to make it print the highest value from the comparisons? var fractured={ //Properties title:"Fractured", release:2019, rating:8, format:"digital", genre:[ "Mystery","Sci-Fi","Western"] }; var countdown={ title:"Countdown", release:2018, rating:5, genre:["Sci-Fi","Mystery","Western"] } var bloodshot={ title:"Bloodshot", release:2020, rating:6, format:"digital", genre:["Sci-Fi","Action"] } var nmovies=new Array(fractured,life,crisis,revenant,bloodshot,countdown); //Sorting the movies by rating console.log(nmovies.sort((a, b) => { if (a.rating < b.rating) return -1; if (a.rating > b.rating) return 1; return 0; }));

28th Feb 2021, 2:21 PM
Timothy Njiru
Timothy Njiru - avatar
9 Answers
+ 3
You need to swap the condition in your sort callback if (a.rating > b.rating) return -1; if (a.rating < b.rating) return 1; return 0;
28th Feb 2021, 2:33 PM
Ipang
+ 3
Alright then ... 👌 I assume you're accessing SoloLearn through the web? cause in the app, each post has a menu to its right, which we use to edit, or delete a post (an FYI).
28th Feb 2021, 2:54 PM
Ipang
+ 2
It's yours, I have zero access to it. Try and see for a menu buttonz something like hamburger menu to the right of your post ... Still not clear whether you were searching for highest rating or sorting in descending order by rating though.
28th Feb 2021, 2:46 PM
Ipang
+ 1
Thanks, now i just want the sort function to give me that number which is the highest from the rating
28th Feb 2021, 2:35 PM
Timothy Njiru
Timothy Njiru - avatar
+ 1
I don't understand what you mean by "give me that number which is the highest from the rating" Are you into sorting or searching anyways?
28th Feb 2021, 2:37 PM
Ipang
+ 1
if u sort them properly the first element of the array should be the highest: here var fractured={ //Properties title:"Fractured", release:2019, rating:8, format:"digital", genre:[ "Mystery","Sci-Fi","Western"] }; var countdown={ title:"Countdown", release:2018, rating:5, genre:["Sci-Fi","Mystery","Western"] } var bloodshot={ title:"Bloodshot", release:2020, rating:6, format:"digital", genre:["Sci-Fi","Action"] } var nmovies=new Array(fractured,bloodshot,countdown); //Sorting the movies by rating nmovies.sort((a, b) => { if (a.rating < b.rating) return 1; if (a.rating > b.rating) return -1; return 0; }); for(var i=0; i<nmovies.length ;i++){ console.log(nmovies[i].rating+" "+nmovies[i].title); }
28th Feb 2021, 2:40 PM
Diego de la Fuente Curaqueo
Diego de la Fuente Curaqueo - avatar
+ 1
True, i just accessed the element after sortin, i have looked and there is no delete button in the question, can you delete it please
28th Feb 2021, 2:43 PM
Timothy Njiru
Timothy Njiru - avatar
+ 1
I really cant find it but thanks i got the number after your code did a push to index zero for the highest number
28th Feb 2021, 2:49 PM
Timothy Njiru
Timothy Njiru - avatar
+ 1
Am gonna try to delete it via phone, yes from the web is my AP
28th Feb 2021, 3:04 PM
Timothy Njiru
Timothy Njiru - avatar