6
  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

2 Answers 2

15

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

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

Stackblitz

Sign up to request clarification or add additional context in comments.

Comments

6

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.