Sorting an Array of GameObjects by values inside these GOs

I am currently trying to sort my inventory slot arrays by the conditions of the items(gameobjects) stored therein.
Some info:

The inventory is a 2-dimensional array of gameobjects. x is the slot number, and y the item number in that slot, so one slot can have multiples of the same item.

The items stored inside are gameobjects with an attached item script that contain a variable “Condition” (integer) from 0 to 100.

Now I want to sort one slot so the items with the best condition is at position 0, second best at position 1, and so on.

I looked around but all I found concerning array sorting were examples were the arrays were mad up of comparable values likes ints or names, not sorted inside a script as in my case.

The JS Array class accepts compare functions in the Sort method, just like in web javascript. You may implement any kind of compare function, like one that compares script variables. Supposing that the script where Condition is declared is called ItemScript.js, for instance, create a compare function like this one:

function CompareCondition(itemA: GameObject, itemB: GameObject): int {
  var scriptA = itemA.GetComponent(ItemScript);
  var scriptB = itemB.GetComponent(ItemScript);
  if (scriptA.condition > scriptB.condition) return 1;
  if (scriptA.condition < scriptB.condition) return -1;
  return 0;
}

Then sort the array like this:

someArray.Sort(CompareCondition);

EDITED: I read whydoidoit’s article about Linq, and found it extremely useful for array manipulation (thanks to @whydoidoit for the valuable hint!). Linq implements SQL-like commands in arrays and other collections, which are very useful for extracting subsets of the collection that satisfy some conditions - and the subsets can also be ordered, what’s the main purpose in this case. Using Linq, you could create an ordered copy of your array like this:

var sortedItems = PickUpSlot.OrderBy(
  function (item){
    return item.GetComponent(ItemScript).Condition;
  }
);

This code saves in the array sortedItems an ordered copy of PickUpSlot. The elements are ordered by the value returned by the function passed to OrderBy - which in this case is the value of Condition found in the script ItemScript in each element of the PickUpSlot array. If you want to reverse the order, use OrderByDescending instead:

var sortedItems = PickUpSlot.OrderByDescending(
  function (item){ 
    return item.GetComponent(ItemScript).Condition;
  }
);

EDITED 2: Returning to the original array sorting approach, you can sort a built-in array with the same compare function - just use System.Array.Sort instead:

System.Array.Sort(someArray, CompareCondition);

This code compares the elements according to the CompareCondition function and physically rearranges them in the specified order. If you want to reverse the order, swap 1 and -1 in the CompareCondition function:

function CompareCondition(itemA: GameObject, itemB: GameObject): int {
  var scriptA = itemA.GetComponent(ItemScript);
  var scriptB = itemB.GetComponent(ItemScript);
  if (scriptA.Condition > scriptB.Condition) return -1;
  if (scriptA.Condition < scriptB.Condition) return 1;
  return 0;
}

This function will cause Null Reference errors if there are null elements in the array, or if any element doesn’t have the script ItemScript. In order to avoid this, a more complex function should be created:

function CompareCondition(itemA: GameObject, itemB: GameObject): int {
  var condA = 0; // Condition A is zero if null item or no script
  if (itemA){
    var scriptA = itemA.GetComponent(ItemScript);
    // get Condition of itemA, if it exists:
    if (scriptA) condA = scriptA.Condition; 
  } 
  var condB = 0; // Condition B is zero for null item or no script
  if (itemB){
    var scriptB = itemB.GetComponent(ItemScript);
    if (scriptB) condB = scriptB.Condition;
  }
  if (condA > condB) return 1;
  if (condA < condB) return -1;
  return 0;
}

#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 ++;

}

}

}