code: def sort_data(data, hparams, train_step):
      """Sorts the dataset according to hyperparameter values
  
      Arguments
      ---------
      data: speechbrain.dataio.dataset.DynamicItemDataset
          the dataset to be sorted
      hparams: dict
          raw hyperparameters
      train_step: dict
          the hyperparameters of the training step
  
      Returns
      -------
      data: speechbrain.dataio.dataset.DynamicItemDataset
          sorted data
      """
      if hparams["sorting"] == "ascending":
          # we sort training data to speed up training and get better results.
          data = data.filtered_sorted(sort_key="duration")
  
      elif hparams["sorting"] == "descending":
          data = data.filtered_sorted(sort_key="duration", reverse=True)
  
      elif hparams["sorting"] == "random":
          pass
  
      else:
          raise NotImplementedError(
              "sorting must be random, ascending or descending"
          )
  
      sample = train_step.get("sample")
      if sample:
          sample_ids = list(data.data_ids)
          if train_step.get("sample_random"):
              random.shuffle(sample_ids)
          sample_ids = sample_ids[:sample]
          data = FilteredSortedDynamicItemDataset(data, sample_ids)
  
      return data
  
imports: from speechbrain.dataio.dataset import FilteredSortedDynamicItemDataset, DynamicItemDataset
  from speechbrain.dataio.sampler import BalancingDataSampler
  from speechbrain.utils.data_utils import undo_padding
  import datasets
  import logging
  import os
  import random
  import speechbrain
  import sys
  from enum import Enum
  from collections import namedtuple
  from hyperpyyaml import load_hyperpyyaml
  from functools import partial
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.utils.pretrained import save_for_pretrained
  from speechbrain.lobes.models.g2p.dataio import enable_eos_bos, grapheme_pipeline, phoneme_pipeline, tokenizer_encode_pipeline, add_bos_eos, get_sequence_key, phonemes_to_label
  from speechbrain.dataio.wer import print_alignments
  from speechbrain.wordemb.util import expand_to_chars
  from io import StringIO
  from speechbrain.utils import hpopt
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: sort_data(data, hparams, train_step) and returns: data