how to sort array of objects in descending order in angular?

37,241

Solution 1

Just pass a function to the sort, based on which you want to sort the array

.sort((a,b) => b - a)

Stackblitz

Solution 2

Your code

this.display=this.addition.sort();

will sort the array in ascending order, to sort in descending order you need to pass a comparator function.

this.display=this.addition.sort((a,b) => b - a);
Share:
37,241

Related videos on Youtube

Asif Ahmed
Author by

Asif Ahmed

A passionate coder, ready for challenges of all kind, i love it when i encounter an error, because that's when i realize i am learning.

Updated on September 28, 2020

Comments

  • Asif Ahmed
    Asif Ahmed about 3 years
      public addition :any = [];
      public display : any = [];
      public object1 =['3','2','1'];
      public value2 =['5','7','9','8'];
      constructor(){}
    
      ngOnInit(){
         this.addition=this.object1.concat(this.value2);
         this.display=this.addition.sort();
      }
    

    how to sort the array in descending order using typescript in angular 2 or above versions Complete code Here