# (Javascript) Will findNearest sort objects into an array?

**URL:** <https://discourse.codecombat.com/t/javascript-will-findnearest-sort-objects-into-an-array/2716>\
**Category:** Uncategorized\
**Created:** [February 15, 2015, 5:36pm UTC](https://discourse.codecombat.com/t/javascript-will-findnearest-sort-objects-into-an-array/2716 "2015-02-15T17:36:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inrideo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/inrideo/32/2043_2.png) [@Inrideo](https://discourse.codecombat.com/u/Inrideo)\
**Post date:** [February 15, 2015, 5:36pm UTC](https://discourse.codecombat.com/t/javascript-will-findnearest-sort-objects-into-an-array/2716/1 "2015-02-15T17:36:51Z")

</div>

If I implement findNearest like this:

var enemies = this.findNearest(this.findEnemies());

will it populate the array in order of nearest? Or is it just for single objects?

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [February 15, 2015, 6:48pm UTC](https://discourse.codecombat.com/t/javascript-will-findnearest-sort-objects-into-an-array/2716/2 "2015-02-15T18:48:35Z")

</div>

`this.findNearest()` returns the (one and only) nearest object from the given array. In your example, you would get the nearest enemy. The array is not modified during this process (which in this case is irrelevant anyways).

---

<div class="post-metadata">

**Author:** ![Inrideo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/inrideo/32/2043_2.png) [@Inrideo](https://discourse.codecombat.com/u/Inrideo)\
**Post date:** [February 15, 2015, 7:12pm UTC](https://discourse.codecombat.com/t/javascript-will-findnearest-sort-objects-into-an-array/2716/3 "2015-02-15T19:12:14Z")

</div>

Thanks. If it sorted the whole array of enemies by distance it would have changed my approach to the trials.  
Good to know 🙂

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [February 15, 2015, 7:14pm UTC](https://discourse.codecombat.com/t/javascript-will-findnearest-sort-objects-into-an-array/2716/4 "2015-02-15T19:14:03Z")

</div>

There is (should be) a sort method in JavaScript, you might want to google that. Somehow I think that there was a bug with sorting, but I’m not 100% sure. You might as well just try it and see what happens.

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [February 15, 2015, 7:24pm UTC](https://discourse.codecombat.com/t/javascript-will-findnearest-sort-objects-into-an-array/2716/5 "2015-02-15T19:24:17Z")

</div>

Nope. sorting works just fine.
