How to add colliders to an array, and pick the oldest one?

Im trying to make a tower defense game and i need a tower targetting system that picks the oldest object (the enemy closest to the end) in its range and shoots at it until the object dies, then picks a new target. Each enemy object has a “age” variable that’s its age in seconds.

How do i add the enemies in range to my enemiesinrange array? I need it to return the oldest object so that the turret can target it.

The following part doesn’t compile:

if(enemiesinrange.length > 0) {
        var oldestEnemy = enemiesinrange[0];
        var age = Collider.age;

        for (var i = 0; i < array.Count() - 1; i++) {
    if (array*.age > array[oldestIndex].age) {*

oldestIndex = i;
}
}

print(oldestEnemy);
}
Here’s the code of the entire function:
function OnTriggerStay (other : Collider) {

if(other.gameObject.CompareTag(“Enemy”)){
selectedTarget = other;
isidle=0;
if(enemiesinrange.length > 0) {
var oldestEnemy = enemiesinrange[0];
var age = Collider.age;

for (var i = 0; i < array.Count() - 1; i++) {
if (array*.age > array[oldestIndex].age) {*
oldestIndex = i;
}
}

print(oldestEnemy);
}
}

It’s maybe easiest to just sort the list. Something like this:

Array.Sort(enemiesinrange, delegate(MyEnemyType a, MyEnemyType b)
{
    if(a.age > b.age)
        return 1;
    else if(a.age < b.age)
        return -1;
    else
        return 0;
});
var oldestEnemy = enemiesinrange[0];