code: def grouped_and_ordered_indexes(
      targets: Sequence[int],
      patterns_indexes: Union[None, Sequence[int]],
      bucket_classes: bool = True,
      sort_classes: bool = False,
      sort_indexes: bool = False,
  ) -> Union[List[int], None]:
      """
      Given the targets list of a dataset and the patterns to include, returns the
      pattern indexes sorted according to the ``bucket_classes``,
      ``sort_classes`` and ``sort_indexes`` parameters.
  
      :param targets: The list of pattern targets, as a list.
      :param patterns_indexes: A list of pattern indexes to include in the set.
          If None, all patterns will be included.
      :param bucket_classes: If True, pattern indexes will be returned so that
          patterns will be grouped by class. Defaults to True.
      :param sort_classes: If both ``bucket_classes`` and ``sort_classes`` are
          True, class groups will be sorted by class index. Ignored if
          ``bucket_classes`` is False. Defaults to False.
      :param sort_indexes: If True, patterns indexes will be sorted. When
          bucketing by class, patterns will be sorted inside their buckets.
          Defaults to False.
  
      :returns: The list of pattern indexes sorted according to the
          ``bucket_classes``, ``sort_classes`` and ``sort_indexes`` parameters or
          None if the patterns_indexes is None and the whole dataset can be taken
          using the existing patterns order.
      """
      if bucket_classes:
          return _indexes_grouped_by_classes(
              targets,
              patterns_indexes,
              sort_indexes=sort_indexes,
              sort_classes=sort_classes,
          )
  
      if patterns_indexes is None:
          # No grouping and sub-set creation required... just return None
          return None
      if not sort_indexes:
          # No sorting required, just return patterns_indexes
          return tensor_as_list(patterns_indexes)
  
      # We are here only because patterns_indexes != None and sort_indexes is True
      patterns_indexes = tensor_as_list(patterns_indexes)
      result = list(
          patterns_indexes
      )  # Make sure we're working on a copy
      result.sort()
      return result
  
imports: from collections import OrderedDict, defaultdict, deque
  from typing import TYPE_CHECKING, Any, Callable, Generic, Iterator, List, Iterable, Mapping, Optional, Sequence, TypeVar, Union, Dict, SupportsInt
  import warnings
  import torch
  from torch import Tensor
  from torch.utils.data import Subset, ConcatDataset
  from .data import AvalancheDataset
  from .data_attribute import DataAttribute
  from .dataset_definitions import ISupportedClassificationDataset
  from .dataset_utils import SubSequence, find_list_from_index
  from .flat_data import ConstantSequence
  from .transform_groups import TransformGroupDef, TransformGroups, XTransform, YTransform
label_desc: Use image node_id 3 for calling a global function with example usage: grouped_and_ordered_indexes(targets, patterns_indexes, bucket_classes, sort_classes, sort_indexes) and returns: result, _indexes_grouped_by_classes, None, tensor_as_list