protected function stableWeightSort(array &$array)
    {
        foreach ($array as $i => &$value) {
            $value["key"] = $i;
        }
        unset($value);

        usort($array, function(array $v1, array $v2) {
            $byWeight = $v1["weight"] - $v2["weight"];
            if ($byWeight === 0) {
                return $v1["key"] - $v2["key"];
            }
            return $byWeight;
        });
    }