-2

Array of objects

this.sampleArray = [{ID: 176, active: true },
 {ID: 181, active: false },
 {ID: 186, active: true }];

How to use Angular TypeScript to order them in descending order.

I am using the following:

const result= _.sortBy(_.where(this.sampleArray, 'ID')).reverse();

But this is not reliable as the ID order keep on changing from descending to ascending. I am using underscore library

Pearl
  • 5,485
  • 6
  • 36
  • 55

1 Answers1

1

YOu can use the simple sort method provided by the javascript.

this.sampleArray = [{ID: 176, active: true },
 {ID: 181, active: false },
 {ID: 186, active: true }];

this.sampleArray.sort((a, b) => b.ID - a.ID);

Working code:

const arr = [{ID: 176, active: true },
 {ID: 181, active: false },
 {ID: 186, active: true },
 {ID: 183, active: true }];
 
 arr.sort((a, b) => b.ID - a.ID);
 
 console.log(arr)
Jasdeep Singh
  • 7,113
  • 1
  • 7
  • 25