docs: def sort_stats(stats,
                 sortedby='cpu_percent',
                 sortedby_secondary='memory_percent',
                 reverse=True):
      """Return the stats (dict) sorted by (sortedby).
  
      Reverse the sort if reverse is True.
      """
      if sortedby is None and sortedby_secondary is None:
          # No need to sort...
          return stats
  
      # Check if a specific sort should be done
      sort_lambda = _sort_lambda(sortedby=sortedby,
                                 sortedby_secondary=sortedby_secondary)
  
      if sort_lambda is not None:
          # Specific sort
          try:
              stats.sort(key=sort_lambda, reverse=reverse)
          except Exception:
              # If an error is detected, fallback to cpu_percent
              stats.sort(key=lambda process: (weighted(process['cpu_percent']),
                                              weighted(process[sortedby_secondary])),
                         reverse=reverse)
      else:
          # Standard sort
          try:
              stats.sort(key=lambda process: (weighted(process[sortedby]),
                                              weighted(process[sortedby_secondary])),
                         reverse=reverse)
          except (KeyError, TypeError):
              # Fallback to name
              stats.sort(key=lambda process: process['name'] if process['name'] is not None else '~',
                         reverse=False)
  
      return stats