Unity JS array custom sort

Hello :slight_smile: I’ve been all over Unity answers tyring to answer this one myself, but nothing I come across appears to be working…

The following is what I’d like to do:

var arr = [1, 2, 3, 4, 6, 5];
arr.Sort(sortFunc);

function sortFunc(a, b)
{
return b-a;
}
Debug.Log(arr);//[6, 5, 4, 3, 2, 1]

However, the actual syntax for applying a custom sort function to a JS generic array is proving difficult to find. Anyone willing to offer some help? (P.S. this is for Unity 2, NOT 3 (compatability reasons, hard to explain))

#EDIT 1:

The array I am sorting is constructed like so:

var arr = ["a", "b", "c", "d"];

//Next, I run arr through a function that returns:

var arr = [["a", 1], ["b", 2], ["c", 3], ["d", 4]];

//Finally, try to sort it as shown above.

My sorter is arranged like so:

function SortIEByType(a : Array, b : Array)
{
	var ra = parseInt(a[0][1]);
	var rb = parseInt(b[0][1]);
	return ra-rb;
}

I have specified the a and b as Array as you did for integers… No effect :frowning: I also had a : int appended to my method, but that did not work either.

Any ideas?

Well, I got it in 3.5 - don’t know if this works in 2.x

var arr = new Array(1, 2, 3, 4, 6, 5);
arr.Sort(sortFunc);

function sortFunc(a:int, b:int){ // I had to declare the types
  return b-a;
}

Debug.Log(arr);//[6, 5, 4, 3, 2, 1]

EDITED: For custom types, you should create a class:

class Element { // declare a class for the array elements:
  var name:String;
  var num:int;
  
  function Element(name:String, num:int){
    this.name = name;
    this.num = num;
  }
}

var arr = new Array(new Element("a",1), new Element("b",2), new Element("c",3),  new Element("d",4));

arr.Sort(sortFunc);

function sortFunc(a:Element, b:Element){
  return b.num-a.num;
}

for (var el:Element in arr) Debug.Log(el.name+" "+el.num);

Your second function could receive an array of Element and assign their num field, then you could call Sort.

NOTE: You must declare arr = new Array(), or a built-in array will be created instead - and built-in arrays have no Sort method, nor are resizable.

#pragma strict

var segments : GameObject;
private var words : int;

function Start () {

bullets = 0;

// finds all gameObjects with the tag bullet

segments = GameObject.FindGameObjectsWithTag(“bullet”);

// if all gameObjects with the tag “bullet” is named “0”, “1”, “2”, … then this will sort the gameObjects in asending oreder, provided that the number of gameObjects with the tag bullet is 10. (you could use segments.lenght)

for(var i = 0; i < 10 - 1; i++){
for(var j = 10 - 1; j > i; j–){
var temp;
if( parseInt(segments[j].name) < parseInt(segments[j - 1].name)){
temp = segments[j];
segments[j] = segments[j - 1];
segments[j - 1] = temp;
}
}
}
}

function Update () {

//Starts an animation for one bullet (starting with bullet 0) when the “s” key is presed by activating an transition in the bullets animator witht the trigger “bom”. The same trigger is used for all bullets.

if(bullets < 10){

if(Input.GetKeyDown(“s”)){

Debug.Log(segments[worbullets].gameObject.name);

segments[bullets].gameObject.GetComponent(Animator).SetTrigger("bom ");

words ++;

}

}

}