# Sorting help

**URL:** https://forum.kirupa.com/t/sorting-help/228892
**Category:** flash
**Created:** [June 12, 2007, 10:01am UTC](https://forum.kirupa.com/t/sorting-help/228892 "2007-06-12T10:01:06Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![gooms9](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@gooms9](https://forum.kirupa.com/u/gooms9)
#### Post date: [June 12, 2007, 10:01am UTC](https://forum.kirupa.com/t/sorting-help/228892/1 "2007-06-12T10:01:06Z")

</div>

Hey, so i’ve got a movie clip, and 4 points around it, and what i’m trying to do is to make the movie clip move towards the closest target. I’m slightly stuck at the moment. I can make my movie clip move to any specific point, and I can find the distance between my movie clip and any point, but i’m having trouble sorting the different points based on their distance from my movie clip. Heres what i’ve got at the moment.

```auto
point1={x:20,y:20};
point2={x:530,y:20};
point3={x:20,y:380};
point4={x:530,y:380};

pointA = [point1, point2, point3, point4];
ballA = [];

for(i=0; i<5; i++)
{
        ball = attachMovie("ball", "ball"+_root.getNextHighestDepth(), _root.getNextHighestDepth())
        ball._x = Math.random()*450+50;
        ball._y = Math.random()*300+50;
        ballA.push(ball);
                
        _root.onEnterFrame = function() {
            move(pointA[1]);
        }
}

function move(target)
{
    for (i=0; i < ballA.length; i++) {
        if (target.x < ballA*._x)
            ballA*._x -= 2;
        if (target.x > ballA*._x)
            ballA*._x += 2;
        if (target.y < ballA*._y)
            ballA*._y -= 2;
        if (target.y > ballA*._y)
            ballA*._y += 2;
    }
}

```

I’m checking the distance between my ball and the target with this…

Math.sqrt((ball.\_x-pointA[0].x)^2+(ball.\_y-pointA[0].y)^2);

Any help would be appreciated.
