code: def fetch_and_validate_grid_sort(a_node, key, sort_by, decreasing):
      # key='kmeans_prostate_grid', sort_by='totss', decreasing=TRUE)
      grid = a_node.grid(
          key=key, sort_by=sort_by, decreasing=decreasing
      )
      training_metrics = grid["training_metrics"]
  
      # check sorting:
      criteria = []
      # Unfortunately, we use mixed case in the JSON and lower case in the back end. . .
      for mm in training_metrics:
          for k, v in mm.items():
              if k.lower() == sort_by:
                  criteria.append(v)
                  break
      unsorted = list(criteria)
      criteria.sort(reverse=decreasing)
  
      # print("criteria sorted: " + repr(criteria))
      # print("original: " + repr(unsorted))
  
      assert unsorted == criteria, (
          "FAIL: model metrics were not sorted correctly by criterion: "
          + key
          + ", "
          + sort_by
          + ", decreasing: "
          + decreasing
      )
  
      for i in range(len(grid["model_ids"])):
          assert (
              grid["model_ids"][i]["name"]
              == training_metrics[i]["model"]["name"]
          ), (
              "FAIL: model_ids not sorted in the same order as training_metrics for grid: "
              + key
              + ", index: "
              + str(i)
          )
  
imports: import sys, os, time, json, datetime, errno, stat, getpass, requests, pprint
  import h2o
label_desc: Use image node_id 26 for calling a global function with example usage: fetch_and_validate_grid_sort(a_node, key, sort_by, decreasing) without return types