code: def hclusterSort(vectors, dist_fn):
      distances = []
      vecs = list(vectors)[:]
      for i in range(0, len(vecs)):
          for j in range(i + 1, len(vecs)):
              distances.append(
                  {
                      "from": vecs[i],
                      "to": vecs[j],
                      "dist": dist_fn(vecs[i], vecs[j]),
                  }
              )
      distances = sorted(distances, key=lambda d: d["dist"])
      excluding_names = []
  
      while len(distances) > 0:
          min_dist = distances[0]
  
          new_vec = {
              "name": str(min_dist["from"]["name"])
              + "|"
              + str(min_dist["to"]["name"]),
              "members": min_dist["from"].get(
                  "members", [min_dist["from"]]
              )
              + min_dist["to"].get("members", [min_dist["to"]]),
          }
  
          excluding_names = [
              min_dist["from"]["name"],
              min_dist["to"]["name"],
          ]
  
          vecs = list(
              filter(lambda v: v["name"] not in excluding_names, vecs)
          )
          distances = list(
              filter(
                  lambda dist: (
                      dist["from"]["name"] not in excluding_names
                  )
                  and (dist["to"]["name"] not in excluding_names),
                  distances,
              )
          )
  
          for v in vecs:
              total = 0
              for vi in v.get("members", [v]):
                  for vj in new_vec["members"]:
                      total += dist_fn(vi, vj)
              distances.append(
                  {
                      "from": v,
                      "to": new_vec,
                      "dist": total
                      / len(v.get("members", [v]))
                      / len(new_vec["members"]),
                  }
              )
  
          vecs.append(new_vec)
          distances = sorted(distances, key=lambda d: d["dist"])
  
      return vecs
  
file_path: turicreate/src/python/turicreate/toolkits/_evaluate_utils.py
imports: from six.moves import reduce
  import math
  import turicreate
label_desc: Use image node_id 5 for calling a global function with example usage: hclusterSort(vectors, dist_fn) and returns: vecs