MZ Sort method not correctly sorting..?

I'm sorting the charge times(this is the fill percentage of the turn bars, the acceleration is how fast they fill to 100%). It's part of my process to figure out how to build a Turn Priority Window.
JavaScript:
function sortChargeTimeForAll() {
    const allBattlers = $gameParty.members().concat($gameTroop.members());
    let allChargeTime = allBattlers.map(battler => battler.tpbChargeTime());
 
    return allChargeTime.sort((a, b) => a > b);
};

Result:
Spoiler
  1. 0: 0.28787919961617514
  2. 1: 0.27468077504089805
  3. 2: 0.03881201245200878 <--- this should go last
  4. 3: 0.1327129416040321
  5. 4: 0.10528425551945243
When a player can select a command(full bar = 1)
  1. 0: 1
  2. 1: 0.9456361500326629
  3. 2: 0.5839310813100546 <--- Same element
  4. 3: 0.6778320104620777
  5. 4: 0.6504033243774981

I can't figure out why sort puts element 2 in the middle when it's clearly not larger than element 3.
Upon further observations of the log, it's been determined that this is due to sort being a bit too slow(probably doesn't help that the function itself is basically 3 loops in addition to sort) as the charge times update rather quickly. The arrangement will always be out of order in some way and sort tries its best to arrange them properly.

EdIt : Apparently there's something I'm not understanding with the sort function. I've added a condition to sort only when one of the characters has a turn (the Attack command window is present) which means all charge times for other characters have stopped. Thinking this will allow Sort to stabilize the arrangement but unfortunately the same element is not in the right order.

Edit 2, Edit's Revenge: Changed the sorting from a > b to a - b and the arrangement is in the proper order. However this is from smallest to largest and I need largest to smallest.
I had a similar problem with Ace, where the window would show a different value to what I had calculated.
it turned out that I was reading +1/-1 loop compared to what the engine was doing.
could this be a similar case? you're reading a previous loop when the engine has already processed it?

it should be specially true if all your increments are asymmetrical or unbalanced: you order one list, and then add different increments to the lot (some increase by 1, some by 2, some by 0.5), and it doesn't come out quite as expected.
Edit 2, Edit's Revenge: Changed the sorting from a > b to a - b and the arrangement is in the proper order. However this is from smallest to largest and I need largest to smallest.
Hi there!

Did you try just changing it to b - a?

return allChargeTime.sort((a, b) => b - a);
I came to say that I did just that LOL. This is solved now.

May as well share the result(note that this doesn't reveal who is next to attack, as there are other factors that alter turn order that I haven't accounted for yet):

JavaScript:
function getBattlersInOrderOfChargeTime() {
    const allBattlers = $gameParty.members().concat($gameTroop.members());
    let sortByCharge = [];
 
    for (let i = 0; i < allBattlers.length; i++) {
        if (allBattlers[i]._tpbState === "charging") {
            sortByCharge.push(allBattlers[i]);
        };
    };
    return sortByCharge.sort((a, b) => b._tpbChargeTime - a._tpbChargeTime);
};
For those who are curious why it's a - b instead of a > b, that's because the sort function expects a number which is:
  • positive when the elements are in descending order.
  • negative when the elements are in ascending order.
  • zero when the elements are of equal order.
However, a > b does not return a number. Instead, it either returns true or false.

By default, the sort function will try and order elements in ascending order, so a - b will sort the array from smallest to largest, and b - a will sort it the other way around.

However, while this works with numbers, it won't work with text. Fortunately, now that we know the sort function expects a number, we can modify our original code to make it work for text, for example:
a > b ? 1 : a < b ? -1 : 0 will sort the array in alphabetic order. To get the reverse order, you only need to switch 1 and -1, or a > b and a < b.

To explain what the above code does, it's the ternary operator:
condition ? a : b will give us a if condition is true, and b otherwise. We simply chained two of those operators together in order to get 3 different results.

If that's too complex (and it can get very confusing very fast), we can also wrap our function in a code block and use a couple of simple if statements:
JavaScript:
return allChargeTime.sort((a, b) => {
  if (a > b) {
    return 1;
  } else if (a < b) {
    return -1;
  }

  return 0;
});

Original thread: https://forums.rpgmakerweb.com/threads/sort-method-not-correctly-sorting.147577/