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
Post a Comment