MZ Get enemy battlers with lowest hp in troop

I have a troop with big slime enemy and smaller slime enemies. The big slime has an attack that revives and/or heals the small slimes. I want the attack to affect 3 small slimes. Priority is that if any slime is dead it revives it (full hp) and if there are no more dead slimes then I want it to heal them starting with the one with the lowest hp.

I wrote some javascript (see below) for conditions depending on the amount of dead slimes. I guess what I'd want to add here is to sort the aliveMembers array depending on their current hp (lowest first) and then pick the first x (depending on condition) to heal. But I don't know how to sort it.

Spoiler
let aliveGuys = $gameTroop.aliveMembers()
let deadGuys = $gameTroop.deadMembers()

if (deadGuys.length == 0) {
// HEAL THREE
}

if (deadGuys.length == 1) {
deadGuys[1].removeState(1);
deadGuys[1].gainHp(30);
// HEAL TWO
}

if (deadGuys.length == 2) {
deadGuys[1].removeState(1);
deadGuys[1].gainHp(30);
deadGuys[2].removeState(1);
deadGuys[2].gainHp(30);
// HEAL ONE
}

if (deadGuys.length >= 3) {
deadGuys[1].removeState(1);
deadGuys[1].gainHp(30);
deadGuys[2].removeState(1);
deadGuys[2].gainHp(30);
deadGuys[3].removeState(1);
deadGuys[3].gainHp(30);
}

EDIT: Also realized that my array indexes should start at 0, instead of 1
sort the aliveMembers array depending on their current hp (lowest first)
Code:
let aliveGuys = $gameTroop.aliveMembers();
aliveGuys.sort(function(a, b){return a.hp - b.hp});
Cool thanks!

Btw I started thinking, can it cause some problems that I sort the aliveMembers array, which the game uses itself? Like I read here earlier that even if I declare that aliveGuys variable, it's not a new array but just a reference to the existing one or something, I don't know?

EDIT: typo, aliveGuys instead of deadGuys
You're right, the first line should use clone()
You're right, the first line should use clone()
That wouldn't do any harm, but for aliveMembers it should be OK without, because it uses filter to return a shallow copy of members (without the dead guys).

If you used $gameTroop.members(), however, then a clone (shallow copy) would be recommended, since that just returns $gameTroop._enemies.

Plugins could change that...but plugins could change basically anything in the engine.
One more question about this. So I want to remove the big slime from the aliveMembers array, so that it won't be healed in any situation (only the small slimes). But I don't understand what is the data that is inside the array?

let aliveGuys = $gameTroop.aliveMembers();
let bigSlimeIndex = aliveGuys.indexOf(??????????);
aliveGuys.splice(bigSlimeIndex, 1);
I think you can do something like this to filter out an enemy with a particular id. [This is MV by the way].

Code:
$gameTroop.aliveMembers().filter(function(x) { return x._enemyId != 17})
But I don't understand what is the data that is inside the array
entire battlers

what TESTOSTERONE said will work perfectly fine, but if you wanted to continue your attempt by splicing the array, youd use
Code:
let bigSlimeIndex = aliveGuys.indexOf($gameTroop.members()[big slime's troop index])
Okay thanks!
I was supposed to learn Javascript basics already a year ago, but alas.
I think this would be the gist of it, right?
JavaScript:
$gameTroop.aliveMembers()
    .sort((a, b) => a.hp - b.hp)
    .filter(c => c.battlerName() !== 'Big Slime');
You could use anything on the filter, I just figured you wanted Big Slime to chill out for a bit, haha.

If you wanted to take the reduce route, it would be something like this (using enemy ID 1):
JavaScript:
$gameTroop.aliveMembers()
    .sort((a, b) => a.hp - b.hp)
    .reduce((acc, cur) => {
        if (cur.enemyId() === 1) {
            acc.push(cur);
            return acc;
        } else return acc;
    }, []);
I think this would be the gist of it, right?
You would want to use c.name
battlerName() returns the filename of the sprite. Or, as in your second example, reference the enemyId()
Ah, good to know. I thought it was the same, but then realized my file name was set the same as the database name. Thanks!
For an enemy name check it'd be .originalName(), since .name() includes letters used to distinguish duplicate enemies with the same base name.

(I'd just go with the ID check, personally. Name lookups can complicate localisations & redesigns.)

Original thread: https://forums.rpgmakerweb.com/threads/get-enemy-battlers-with-lowest-hp-in-troop.168886/