docs: private static List<HostAddress> sortSRVRecords(List<SRVRecord> records) {
          // RFC 2782, Usage rules: "If there is precisely one SRV RR, and its Target is "."
          // (the root domain), abort."
          if (records.size() == 1 && records.get(0).getFQDN().isRootLabel())
              return Collections.emptyList();
  
          // sorting the records improves the performance of the bisection later
          Collections.sort(records);
  
          // create the priority buckets
          SortedMap<Integer, List<SRVRecord>> buckets = new TreeMap<Integer, List<SRVRecord>>();
          for (SRVRecord r : records) {
              Integer priority = r.getPriority();
              List<SRVRecord> bucket = buckets.get(priority);
              // create the list of SRVRecords if it doesn't exist
              if (bucket == null) {
                  bucket = new LinkedList<SRVRecord>();
                  buckets.put(priority, bucket);
              }
              bucket.add(r);
          }
  
          List<HostAddress> res = new ArrayList<HostAddress>(records.size());
  
          for (Integer priority : buckets.keySet()) {
              List<SRVRecord> bucket = buckets.get(priority);
              int bucketSize;
              while ((bucketSize = bucket.size()) > 0) {
                  int[] totals = new int[bucketSize];
                  int running_total = 0;
                  int count = 0;
                  int zeroWeight = 1;
  
                  for (SRVRecord r : bucket) {
                      if (r.getWeight() > 0) {
                          zeroWeight = 0;
                          break;
                      }
                  }
  
                  for (SRVRecord r : bucket) {
                      running_total += (r.getWeight() + zeroWeight);
                      totals[count] = running_total;
                      count++;
                  }
                  int selectedPos;
                  if (running_total == 0) {
                      // If running total is 0, then all weights in this priority
                      // group are 0. So we simply select one of the weights randomly
                      // as the other 'normal' algorithm is unable to handle this case
                      selectedPos = (int) (Math.random() * bucketSize);
                  } else {
                      double rnd = Math.random() * running_total;
                      selectedPos = bisect(totals, rnd);
                  }
                  // add the SRVRecord that was randomly chosen on it's weight
                  // to the start of the result list
                  SRVRecord chosenSRVRecord = bucket.remove(selectedPos);
                  res.add(chosenSRVRecord);
              }
          }
  
          return res;
      }