order - Typescript sorting array with objects on multiple properties -


i sort array objects have multiple properties. objects have string called name , boolean called mandatory.

first want sort on age, next on name.

how do this?

ordering age easy...:

this.model.mylist.sort((obj1: iobj, obj2: iobj => {     if (obj1.age < obj2.age) {         return -1;     }     if (obj1.age > obj2.age) {         return 1;     }     return 0; }); 

well, add comparation case when 2 age values same. should work:

this.model.mylist.sort((obj1: iobj, obj2: iobj) => {     if (obj1.age < obj2.age) {         return -1;     }     if (obj1.age > obj2.age) {         return 1;     }      return obj1.name.localecompare(obj2.name); }); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -