-1

i am using the following code to get distance between multiple address from a single location, after then i push the result to my html, which works fine...but i want to be able to get each distance for each user and also to display the closest users only.

data.forEach( (element) => {const distance = this.distanceInKmBetweenEarthCoordinates(this.Senderlat,this.Senderlng,parseFloat(element.lat),parseFloat(element.lng));


        this.drivers.push(element);

    });

I am using this to display the loop in my html file, which works well..but i need to sort the data by a field name of distance or item.distance so that the closest person comes up first before orders

   <ion-item lines="none" *ngFor="let item of drivers>
shine odigie
  • 156
  • 1
  • 6
  • https://stackoverflow.com/questions/18883601/function-to-calculate-distance-between-two-coordinates - check this out. – Apps May 22 '21 at 03:48
  • Thanks for this..i am able to get the distance betweeen all the cordinates...but i need to display each distance in my html in order of the closest – shine odigie May 22 '21 at 09:39

1 Answers1

0

After you get the array of distances (in whatever unit)

distances.sort((dist1,dist2)=> dist2-dist1);

This takes care of sorting the original array.

<div *ngFor="let distance of distances; let i=index">
{{index}} {{distance}} <!-- prints the value from the sorted array-->
</div>
Apps
  • 3,284
  • 8
  • 48
  • 75