fix: handle parenthetical qualifiers and multi-comma server names

Parse server names more robustly for sorting:
- "City, Country, Provider" → use second part as country
- "City, Country (1) (Hetzner)" → strip parentheticals from country
- "Frankfurt, Germany (FRA01)" → country is "Germany" not "Germany (FRA01)"
This commit is contained in:
valer23 2026-04-17 10:39:33 +02:00 committed by sstidl
parent 42d700cf08
commit 69fa7a4edb
2 changed files with 44 additions and 19 deletions

View file

@ -230,17 +230,30 @@ function populateDropdown(servers) {
});
}
// Sort servers by country, then by city within the same country
// Sort servers by country, then by city within the same country.
// Name formats: "City, Country", "City, Country (qualifier)", "City, Country, Provider", "Country"
const parseServerName = (name) => {
const parts = (name || "").split(",").map((s) => s.trim());
let country, city;
if (parts.length >= 3) {
// "City, Country, Provider" — use second part as country
country = parts[1];
city = parts[0];
} else if (parts.length === 2) {
country = parts[1];
city = parts[0];
} else {
country = parts[0];
city = "";
}
// Strip parenthetical qualifiers for sorting: "Germany (1) (Hetzner)" → "Germany"
country = country.replace(/\s*\([^)]*\)\s*/g, "").trim();
return { country, city };
};
const sorted = [...servers].sort((a, b) => {
const nameA = a.name || "";
const nameB = b.name || "";
const commaA = nameA.lastIndexOf(",");
const commaB = nameB.lastIndexOf(",");
const countryA = commaA >= 0 ? nameA.substring(commaA + 1).trim() : nameA;
const countryB = commaB >= 0 ? nameB.substring(commaB + 1).trim() : nameB;
const cityA = commaA >= 0 ? nameA.substring(0, commaA).trim() : "";
const cityB = commaB >= 0 ? nameB.substring(0, commaB).trim() : "";
return countryA.localeCompare(countryB) || cityA.localeCompare(cityB);
const pa = parseServerName(a.name);
const pb = parseServerName(b.name);
return pa.country.localeCompare(pb.country) || pa.city.localeCompare(pb.city);
});
// Populate the list to choose from