浏览代码

feat: sort server list by country name instead of city name

Sort the server dropdown by country first, then by city within the same
country. This makes it easier to find servers in a specific country when
the list is long. Applies to both modern and classic UIs.
valer23 3 月之前
父节点
当前提交
4ec3a755fa
共有 2 个文件被更改,包括 21 次插入0 次删除
  1. 11 0
      frontend/javascript/index.js
  2. 10 0
      index-classic.html

+ 11 - 0
frontend/javascript/index.js

@@ -230,6 +230,17 @@ function populateDropdown(servers) {
     });
   }
 
+  // Sort servers by country, then by city within the same country
+  servers.sort((a, b) => {
+    const commaA = a.name.lastIndexOf(",");
+    const commaB = b.name.lastIndexOf(",");
+    const countryA = commaA >= 0 ? a.name.substring(commaA + 1).trim() : a.name;
+    const countryB = commaB >= 0 ? b.name.substring(commaB + 1).trim() : b.name;
+    const cityA = commaA >= 0 ? a.name.substring(0, commaA).trim() : "";
+    const cityB = commaB >= 0 ? b.name.substring(0, commaB).trim() : "";
+    return countryA.localeCompare(countryB) || cityA.localeCompare(cityB);
+  });
+
   // Populate the list to choose from
   servers.forEach((server) => {
     const item = document.createElement("li");

+ 10 - 0
index-classic.html

@@ -50,6 +50,16 @@
 					s.selectServer(function (server) {
 						if (server != null) { //at least 1 server is available
 							I("loading").className = "hidden"; //hide loading message
+							//sort servers by country, then by city
+							SPEEDTEST_SERVERS.sort(function (a, b) {
+								var commaA = a.name.lastIndexOf(",");
+								var commaB = b.name.lastIndexOf(",");
+								var countryA = commaA >= 0 ? a.name.substring(commaA + 1).trim() : a.name;
+								var countryB = commaB >= 0 ? b.name.substring(commaB + 1).trim() : b.name;
+								var cityA = commaA >= 0 ? a.name.substring(0, commaA).trim() : "";
+								var cityB = commaB >= 0 ? b.name.substring(0, commaB).trim() : "";
+								return countryA.localeCompare(countryB) || cityA.localeCompare(cityB);
+							});
 							//populate server list for manual selection
 							for (var i = 0; i < SPEEDTEST_SERVERS.length; i++) {
 								if (SPEEDTEST_SERVERS[i].pingT == -1) continue;