You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 26, 2020. It is now read-only.
/*Write a sorting function (sometimes called a custom comparator), that sorts the above array first by the name ascending alphabetically, and in cases where the names are equal sort by descending age. */
varstudents=[
{id: 1,name: "bruce",age: 40},
{id: 2,name: "zoidberg",age: 22},
{id: 3,name: "alex",age: 22},
{id: 4,name: "alex",age: 30},
// Some extra objects to test it for the correct input.
//{ id: 4, name: "alex", age: 17 },
//{ id: 4, name: "alex", age: 19 }
];
//this part is sorting students by name
//this is an exmple of anonymous callback function
students.sort(function(a,b){
varnameA=a.name.toUpperCase();// ignore upper and lowercase
varnameB=b.name.toUpperCase();// ignore upper and lowercase
if(nameA<nameB){
return-1;
}
if(nameA>nameB){
return1;
}
if(nameA=nameB){
//here because we have 2 students with the same name we sort by age
// if you use this function in note below it will double the "bruce name