code: def sort_losses(
      samples: List[dict],
      preds: List[dict],
      by: Union[str, dict] = "loss_total",
  ) -> Tuple[List[dict], List[dict], List[str]]:
      by_copy = deepcopy(by)
      losses_expected = [
          k
          for k in samples[0].losses.keys()
          if "loss" in k and k != "loss_total"
      ]
      if "effdet_total_loss" in losses_expected:
          losses_expected.remove("effdet_total_loss")
  
      if isinstance(by, str):
          loss_check = losses_expected + ["loss_total"]
          assert (
              by in loss_check
          ), f"You must `sort_by` one of the losses. '{by}' is not among {loss_check}"
  
      if isinstance(by, dict):
          expected = ["weighted"]
          assert (
              by["method"] in expected
          ), f"`method` must be in {expected}, got {by['method']} instead."
          if by["method"] == "weighted":
              losses_passed = set(by["weights"].keys())
              losses_expected = set(losses_expected)
              assert (
                  losses_passed == losses_expected
              ), f"You need to pass a weight for each of the losses in {losses_expected}, got {losses_passed} instead."
  
              samples = [
                  get_weighted_sum(s, by["weights"]) for s in samples
              ]
              by = "loss_weighted"
  
      l = list(zip(samples, preds))
      l = sorted(l, key=lambda i: i[0].losses[by], reverse=True)
      sorted_samples, sorted_preds = zip(*l)
      annotations = [el.losses["text"] for el in sorted_samples]
  
      if isinstance(by_copy, dict):
          if by_copy["method"] == "weighted":
              annotations = [
                  f"loss_weighted: {round(s.losses['loss_weighted'], 5)}\n"
                  + a
                  for a, s in zip(annotations, sorted_samples)
              ]
  
      return list(sorted_samples), list(sorted_preds), annotations
  
file_path: icevision/icevision/models/interpretation.py
imports: from icevision.imports import *
  from icevision.utils import *
  from icevision.core import *
  from icevision.data import *
  from icevision.visualize.show_data import show_preds
  from icevision.core.record_components import LossesRecordComponent
label_desc: Use image node_id 2 for calling a global function with example usage: sort_losses(samples, preds, by) and returns: list,  list,  annotations