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;
}