code: def _sort_column(  # noqa: C901
      sort_by: str,
      metric_names: Mapping[str, Iterable[str]],
      param_names: Mapping[str, Iterable[str]],
  ) -> Tuple[str, str, str]:
      sep = ":"
      parts = sort_by.split(sep)
      matches: Set[Tuple[str, str, str]] = set()
  
      for split_num in range(len(parts)):
          path = sep.join(parts[:split_num])
          sort_name = sep.join(parts[split_num:])
          if not path:  # handles ':metric_name' case
              sort_by = sort_name
          if path in metric_names and sort_name in metric_names[path]:
              matches.add((path, sort_name, "metrics"))
          if path in param_names and sort_name in param_names[path]:
              matches.add((path, sort_name, "params"))
      if not matches:
          for path in metric_names:
              if sort_by in metric_names[path]:
                  matches.add((path, sort_by, "metrics"))
          for path in param_names:
              if sort_by in param_names[path]:
                  matches.add((path, sort_by, "params"))
  
      if len(matches) == 1:
          return matches.pop()
      if len(matches) > 1:
          raise InvalidArgumentError(
              "Ambiguous sort column '{}' matched '{}'".format(
                  sort_by,
                  ", ".join(
                      [f"{path}:{name}" for path, name, _ in matches]
                  ),
              )
          )
      raise InvalidArgumentError(f"Unknown sort column '{sort_by}'")
  
imports: from collections import Counter, defaultdict
  from datetime import date, datetime
  from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Literal, Mapping, NamedTuple, Optional, Set, Tuple, Union
  from dvc.exceptions import InvalidArgumentError
  from dvc.log import logger
  from dvc.scm import Git
  from dvc.ui import ui
  from dvc.utils.flatten import flatten
  from .collect import collect
label_desc: Use image node_id 4 for calling a global function with example usage: _sort_column(sort_by, metric_names, param_names) and returns: matches