# Javascript custom sort function

**URL:** https://discussions.unity.com/t/javascript-custom-sort-function/387996
**Category:** Scripting
**Tags:** Scripting
**Created:** [February 22, 2009, 4:13pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996 "2009-02-22T16:13:07Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_58534272](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon\_58534272](https://discussions.unity.com/u/anon_58534272)
#### Post date: [February 22, 2009, 4:13pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/1 "2009-02-22T16:13:07Z")

</div>

I know in regular javascript a custom sort function is pretty simpl to impliment.

For some reason I am unable to make it work with unity javascript though. For example:

arrayName.Sort(customSortFunction);

function customSortFunction (a, b) {  
return (a - b);  
}

Generally you would be able to give a javascript array an argument (function) that controls how the sort work. Is there any way that I can do something similar with unity javascript? Or would I have to do something more complex like reading through the array, and comparing/sorting into a new array manually? I was hoping to be able to do custom sort functions on arrays in place instead.

I have an array of game objects and I need to sort the array based on distance from the player (which is why this matters at all).

Thanks!

---

<div class="post-metadata">

### Author: ![anon\_58534272](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon\_58534272](https://discussions.unity.com/u/anon_58534272)
#### Post date: [February 22, 2009, 8:48pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/2 "2009-02-22T20:48:17Z")

</div>

Seriously no one has ever wanted to just sort something non-alpha numerically in Unity JS? I’m surprised its not a simple thing.

---

<div class="post-metadata">

### Author: ![Adrian](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/adrian/32/93991_2.png) [@Adrian](https://discussions.unity.com/u/Adrian)
#### Post date: [February 22, 2009, 11:01pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/3 "2009-02-22T23:01:33Z")

</div>

This has been disucssed before.  
Like here:  
[http://forum.unity3d.com/viewtopic.php?t=8658](http://forum.unity3d.com/viewtopic.php?t=8658)  
[http://forum.unity3d.com/viewtopic.php?p=77192](http://forum.unity3d.com/viewtopic.php?p=77192)

---

<div class="post-metadata">

### Author: ![anon\_1301986](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon\_1301986](https://discussions.unity.com/u/anon_1301986)
#### Post date: [February 23, 2009, 1:57pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/4 "2009-02-23T13:57:00Z")

</div>

As a matter of fact, it’s even simpler than what those threads describe. You just gotta remember that Unity JS is really .NET, so you have to use System.Array.Sort(array, comparison) instead of array.Sort(comparison).

```auto
var elements : GameObject[];
var player : GameObject;

function Awake () {
	System.Array.Sort(elements, Comparison);
}

function Comparison(x : GameObject, y : GameObject) : int {
	var xDistance = (x.transform.position - player.transform.position).magnitude;
	var yDistance = (y.transform.position - player.transform.position).magnitude;
	return xDistance - yDistance;
}

```

---

<div class="post-metadata">

### Author: ![anon\_58534272](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon\_58534272](https://discussions.unity.com/u/anon_58534272)
#### Post date: [February 23, 2009, 6:33pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/5 "2009-02-23T18:33:54Z")

</div>

tolm, your method is way better than the other examples! Thanks man! I guess I need to get a .NET book. Any recommendations?

---

<div class="post-metadata">

### Author: ![Dreamora](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dreamora/32/91058_2.png) [@Dreamora](https://discussions.unity.com/u/Dreamora)
#### Post date: [February 23, 2009, 6:43pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/6 "2009-02-23T18:43:35Z")

</div>

For anything related to .NET, the first point to start is always: [http://msdn.microsoft.com](http://msdn.microsoft.com)

As second stop, C# 3.0 in a Nutshell is nice as reference.

To learn C# and working with .NET I’m a fan of “Head First C#”

---

<div class="post-metadata">

### Author: ![anon\_1301986](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon\_1301986](https://discussions.unity.com/u/anon_1301986)
#### Post date: [February 23, 2009, 6:59pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/7 "2009-02-23T18:59:35Z")

</div>

What dreamora said; MSDN is really all you need. Just set it to .NET 2.0, since that’s what Unity uses.

---

<div class="post-metadata">

### Author: ![Frido](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@Frido](https://discussions.unity.com/u/Frido)
#### Post date: [December 29, 2010, 3:25pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/8 "2010-12-29T15:25:54Z")

</div>

> [@anon\_1301986](#):
>
> As a matter of fact, it’s even simpler than what those threads describe. You just gotta remember that Unity JS is really .NET, so you have to use System.Array.Sort(array, comparison) instead of array.Sort(comparison).
> 
> ```auto
> var elements : GameObject[];
> var player : GameObject;
> 
> function Awake () {
> System.Array.Sort(elements, Comparison);
> }
> 
> function Comparison(x : GameObject, y : GameObject) : int {
> var xDistance = (x.transform.position - player.transform.position).magnitude;
> var yDistance = (y.transform.position - player.transform.position).magnitude;
> return xDistance - yDistance;
> }
> 
> ```

Thanks Tolm! Your script helpt me a lot in getting a sorted list of objects based on the distance.

---

<div class="post-metadata">

### Author: ![BeforeTheLight](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/beforethelight/32/284809_2.png) [@BeforeTheLight](https://discussions.unity.com/u/BeforeTheLight)
#### Post date: [February 28, 2011, 5:36pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/9 "2011-02-28T17:36:44Z")

</div>

ok a few things to note here.  
1 var testArray = new Array(5,6,4,7,3,8,2,9,1);  
2 function Start(){  
3 **var builtinArray : String[&nbsp;];**  
4 for(var i in testArray){  
5 testArray\*.ToString(“f1”);\*  
_6 }_  
_7 builtinArray = testArray;_  
_8 **System.Array.Sort** (builtinArray, Compare);_  
_9 testArray = builtinArray;_  
_10 print(testArray);_  
_11 }_  
_12_  
_13 function Compare(f, q):int{_  
_14 return (f . **CompareTo** (q));_  
_15 }_  
_Will print 1,2,3,4,5,6,7,8,9. The keys here are for me…Use System.Array.Sort( **Bulitin Array** , Function){ }_  
_I tried buliding a class and could not get a Javascript array to work in this setup. So make a dummy and convert it. Like in line 3-7._  
_Then after conversion in line 9 switch it back. Now most important here to me is it would give me a error everytime i tried to use a \< \> or - sign in the Compare function line 14. Normal Javascript a simple return (f-q); would work fine but not here. I found the Compareto() to do the trick. Now please dont tear me apart I am new to these languages. So please if I am guiding people in the wrong direction let me know but don’t cruicify me if this isn’t exactly correct._

---

<div class="post-metadata">

### Author: ![Dreamora](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dreamora/32/91058_2.png) [@Dreamora](https://discussions.unity.com/u/Dreamora)
#### Post date: [February 28, 2011, 5:41pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/10 "2011-02-28T17:41:06Z")

</div>

The reason you get an error on line 10 is cause you have a string array and strings don’t have \< and \>

also question is really why one would use the Array class. Its nice if you are really new and know basically nothing about programming, but as soon as you do, using List or builtin arrays is the much better way to go.

---

<div class="post-metadata">

### Author: ![BeforeTheLight](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/beforethelight/32/284809_2.png) [@BeforeTheLight](https://discussions.unity.com/u/BeforeTheLight)
#### Post date: [February 28, 2011, 5:48pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/11 "2011-02-28T17:48:06Z")

</div>

I am under the impression that the builtin arrays are not resizeable therefore if i dont know the size i would need to use the array class. Please let me know different because I like using the builtin class much better. And List I haven’t even touched yet. Is it .net list or Javascript? I’ve been using w3Schools tutorials to learn Javascript but I don’t think they have a list there. Thank You for the help.

---

<div class="post-metadata">

### Author: ![anon\_5472729](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon\_5472729](https://discussions.unity.com/u/anon_5472729)
#### Post date: [February 28, 2011, 5:50pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/12 "2011-02-28T17:50:24Z")

</div>

> **[List Class (System.Collections.Generic)](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-9.0&redirectedfrom=MSDN)**
>
> Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

---

<div class="post-metadata">

### Author: ![SwagGamez](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SwagGamez](https://discussions.unity.com/u/SwagGamez)
#### Post date: [December 15, 2015, 7:18pm UTC](https://discussions.unity.com/t/javascript-custom-sort-function/387996/13 "2015-12-15T19:18:32Z")

</div>

This works but I want to use it in a different way.

Can I use this to compare variables in the objects dragged into the array? Example:

I want to drag 8 gameobjects into the array and sort (ascending) the gameoobjects by the variables in each gameobject’s script instead of by each gameobject’s distance. They all have a common script.

So lets just say I drag these into array:

Dodge (gameobject) has the script WaypointSystem with the variable Progress in it.  
Honda (gameobject) has the script WaypointSystem with the variable Progress in it.  
Chevrolet (gameobject) has the script WaypointSystem with the variable Progress in it.  
Ford …  
Mitsubishi  
Hyandai…  
Fiat…  
Yamaha…

How can I sort (ascending) and display the results on GUI?

I am new so I am learning by reverse engineering scripts.
