sortNodesByLatency function

Future<List<String>> sortNodesByLatency(
  1. List<String> urls
)

Sort a list of server URLs by their WebSocket latency (fastest first)

Returns URLs sorted in ascending order of latency. Unreachable servers are placed at the end with double.infinity latency.

Implementation

Future<List<String>> sortNodesByLatency(List<String> urls) async {
  final results = <(String url, double latency)>[];

  await Future.wait(
    urls.map((url) async {
      final latency = await measureWebsocketLatency(url);
      results.add((url, latency));
    }),
  );

  results.sort((a, b) => a.$2.compareTo(b.$2));
  return results.map((r) => r.$1).toList();
}