Ionic 3 how to seperate sort array

#1

Hi,

I need separate descending results for points and rebounds in one API call.

please see below my codes.

  this.http.get('https://api.wh.geniussports.com/v1/basketball/statistics/personmatch/matches/784490/teams/88265?ak=eebd8ae256142ac3fd24bd2003d28782&periodNumber=0&limit=30
')
    .map(res => res.json())
    .subscribe(data => {
      this.dataLeaders = data.response.data;  
     

      //sort api to points
      let pointsL =  this.dataLeaders.sort(function(a, b) {
          return parseInt(b.sPoints) - parseInt(a.sPoints);
      });

      //sort api to rebounds
      let reboundL =  this.dataLeaders.sort(function(c, d) {
        return parseInt(d.sReboundsTotal) - parseInt(c.sReboundsTotal);
      });

      console.log(pointsL);
      console.log(reboundL);
    }, err => {
      console.log(err); 
    });

my problem is it’s both sorting with rebounds instead of sort for points and sort for rebounds

#2

Try the following:

 //sort api to points
this.points = JSON.parse(JSON.stringify(this.dataLeaders));
let pointsL =  this.points.sort(function(a, b) {
          return parseInt(b.sPoints) - parseInt(a.sPoints);
      });
1 Like
#3

@JAR19 Thanks, it works

#4

Glad to help - but be careful - I use the Json.parse/stringify in my code but some times it keeps a reference to the base data - A better solution might be to use angular.copy() which does a deep copy or easier still have two separate http calls and therefore the data is totally separated.