-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
  • 8,373
  • 8
  • 40
  • 59

1 Answers1

2

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,901
  • 1
  • 11
  • 28