From cc6a62a122d0105985b9a9cf0e78b5b599379f09 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 11 Sep 2026 14:37:58 +0200 Subject: [PATCH] feat(match-service): update ranking logic for league table based on balance and previous tie-breakers --- backend/services/matchService.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/backend/services/matchService.js b/backend/services/matchService.js index 14bafd50..8b0c8260 100755 --- a/backend/services/matchService.js +++ b/backend/services/matchService.js @@ -652,11 +652,23 @@ class MatchService { 'id', 'name', 'matchesPlayed', 'matchesWon', 'matchesLost', 'matchesTied', 'setsWon', 'setsLost', 'pointsWon', 'pointsLost', 'tablePoints', 'tablePointsWon', 'tablePointsLost' ], - order: [ - ['tablePointsWon', 'DESC'], // Highest table points first - ['matchesWon', 'DESC'], // Then by matches won - ['setsWon', 'DESC'] // Then by sets won - ] + order: [['id', 'ASC']] + }); + + // A table-points result is ranked by its balance, not just by the + // number of points won: 0:0 (0) must therefore rank ahead of 0:2 + // (-2), which in turn ranks ahead of 0:4 (-4). + teams.sort((left, right) => { + const leftBalance = left.tablePointsWon - left.tablePointsLost; + const rightBalance = right.tablePointsWon - right.tablePointsLost; + if (rightBalance !== leftBalance) return rightBalance - leftBalance; + + // Retain the previous tie-breakers for teams with the same balance. + if (right.tablePointsWon !== left.tablePointsWon) { + return right.tablePointsWon - left.tablePointsWon; + } + if (right.matchesWon !== left.matchesWon) return right.matchesWon - left.matchesWon; + return right.setsWon - left.setsWon; }); // Format table data