# Sorting a MultiDimension Array, OOP, FP7

**URL:** <https://forum.kirupa.com/t/sorting-a-multidimension-array-oop-fp7/173552>\
**Category:** flash\
**Created:** [December 27, 2005, 4:57am UTC](https://forum.kirupa.com/t/sorting-a-multidimension-array-oop-fp7/173552 "2005-12-27T04:57:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwopitz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jwopitz/32/1556_2.png) [@jwopitz](https://forum.kirupa.com/u/jwopitz)\
**Post date:** [December 27, 2005, 4:57am UTC](https://forum.kirupa.com/t/sorting-a-multidimension-array-oop-fp7/173552/1 "2005-12-27T04:57:49Z")

</div>

I have a function inside a class that will basically sort an array by the first elements first sub-element (ie. [0][x]) and return the second sub element based on that information.

I am having the hardest time getting it to work. Partially because I feel that it is inside a class. Also I feel like it sorts the sub elements individually whereas I would need to keep the sub-element pairs together. Here is some of the code. Thanks for any help!

```auto

//this function will sort all the Pts which are stored in the static var points:Array
public function nearest ():Pt {
        var distances:Array = new Array ();
        for (var i:Number = 0; i < points.length; i++) {
            distances.push ([this.distance (points*), points*]);
        }
        distances.sortOn (Array.NUMERIC);
        var nearest_pt = distances[0][1];
        return nearest_pt;
    }
//this is the distance function just for your reference
public function distance (pt:Pt):Number {
        var tx:Number = pt.x - this.x;
        var ty:Number = pt.y - this.y;
        return Math.sqrt (tx * tx + ty * ty);
    }

```
