MV CTB Math (turn order)

So I'm trying to learn the math/mechanics behind a CTB battle system. I found this Googling but found some of it confusing. Specifically, step 1 says that:
Calculate the average speed of each member of the combat, (sum all of the speeds of enemies and characters, then divide the members speed by this sum, you know, average)

TotalSpeed = SUM(Members)

Member.AverageSpeed = Member.Speed / TotalSpeed
However, shouldn't a unit's avg speed be TotalSpeed divided by # of units?

What should my approach be if my current battle system (which I'm trying to edit, not replace) currently manages turn order via an array in which the order of said array elements dictates the turn order? This is the function that handles turn order in LeTBS (Lecode's Tactical Battle System):
JavaScript:
BattleManagerTBS.determineTurnOrderSimple = function () {
    var array = [];
    this._turnOrder = [];
    this._activeIndex = 0;

    this.allPlayableEntities().forEach(function (entity) {
        array.push(entity);
    });
    array = array.sort(function (a, b) {
        return eval(Lecode.S_TBS.turnOrderSortMethod);
    });
    this._turnOrder = array;
    this._turnOrderVisual.set(this._turnOrder);
};
Note that return eval(Lecode.S_TBS.turnOrderSortMethod) simply refers to: b._battler.luk - a._battler.luk
Basically, LUK is my Speed stat. Anyway, I'm simply trying to figure out how to manipulate the array so that the turn order can have a CTB mechanic instead of the basic "fastest to slowest" turn order.
What should my approach be
I would download an existing CTB plugin (Yanfly's is free) and see how it's implemented. It's got a fair number of moving parts.
However, shouldn't a unit's avg speed be TotalSpeed divided by # of units?

As the article itself says, it uses, I quote 'terrible pseudo code', so yes, it is good to keep your logical thinking while implementing it.

The article probably doesn't have in mind apparition of enemies mid-way during combat, so for it # of units basically a constant since it will be the same for everyone. You could replace it by any other constant, it wouldn't change the core thinking.

PS: Yep CTB implementation is closer to a weird ATB, I wouldn't take too much inspiration from it.
Eureka! I believe this is the critical piece I was looking for!~
JavaScript:
BattleManager.ctbTurnOrder = function() {
    var battlers = $gameParty.aliveMembers().concat($gameTroop.aliveMembers());
    battlers.sort(function(a, b) {
      if (a.ctbTicksToReady() > b.ctbTicksToReady()) return 1;
      if (a.ctbTicksToReady() < b.ctbTicksToReady()) return -1;
      return 0;
    });
    return battlers;
};
...and here's how it compares to LeTBS:
JavaScript:
BattleManagerTBS.determineTurnOrderSimple = function () {
    var array = [];
    this._turnOrder = [];
    this._activeIndex = 0;

    this.allPlayableEntities().forEach(function (entity) {
        array.push(entity);
    });
    array = array.sort(function (a, b) {
        return eval(Lecode.S_TBS.turnOrderSortMethod);
    });
    this._turnOrder = array;
    this._turnOrderVisual.set(this._turnOrder);
};
P.S. Lecode.S_TBS.turnOrderSortMethod = b._battler.agi - a._battler.agi

I'm just glad that in both cases (Yanfly & LeCode), the turn order is managed in an array. That makes it easier for me to integrate the CTB mechanic into LeTBS.

Edit: I noticed that in Yanfly's code, the sort function returns either -1, 0, or 1. However, in LeTBS, the sort function will usually return a much wider range of values. Is that something that makes a difference? Or is it perhaps just the positive/negative-ness of the returned value that matters?

Hmm, looks like this is where I should apply the "CTB math" in order to implement CTB in LeTBS:
JavaScript:
BattleManagerTBS.nextTurn = function () {
    do {
        this._activeIndex++;
        if (this._activeIndex >= this._turnOrder.length) {
            this._activeIndex = 0;
            this.onTurnOrderEnd();
        }
    } while (this.activeEntity()._dead);
    if (this.activeEntity().isTurnHidden()) {
        this.nextTurn();
        return;
    }
    this._turnOrderVisual.updateOnNextTurn(this._turnOrder, this._activeIndex);
    this.startTurn();
};
...but instead of this._activeIndex++;, I need to add/subtract the units Speed stat...or is it supposed to be the average Speed?
99% of the sorting methods only care about the sign of the comparaison.

Original thread: https://forums.rpgmakerweb.com/threads/ctb-math-turn-order.143976/