code: def sort_stats(self, *args):
          
  
          criteria = ('classname', 'tsize', 'birth', 'death',
                      'name', 'repr', 'size')
  
          if not set(criteria).issuperset(set(args)):
              raise ValueError("Invalid sort criteria")
  
          if not args:
              args = criteria
  
          def args_to_tuple(obj):
              keys = []
              for attr in args:
                  attribute = getattr(obj, attr)
                  if attr in ('tsize', 'size'):
                      attribute = -attribute
                  keys.append(attribute)
              return tuple(keys)
  
          self._init_sort()
          self.sorted.sort(key=args_to_tuple)
  
          return self
docs: Sort the tracked objects according to the supplied criteria. The
          argument is a string identifying the basis of a sort (example: 'size'
          or 'classname'). When more than one key is provided, then additional
          keys are used as secondary criteria when there is equality in all keys
          selected before them. For example, ``sort_stats('name', 'size')`` will
          sort all the entries according to their class name, and resolve all
          ties (identical class names) by sorting by size.  The criteria are
          fields in the tracked object instances. Results are stored in the
          ``self.sorted`` list which is used by ``Stats.print_stats()`` and other
          methods. The fields available for sorting are:
  
              'classname'
                  the name with which the class was registered
              'name'
                  the classname
              'birth'
                  creation timestamp
              'death'
                  destruction timestamp
              'size'
                  the maximum measured size of the object
              'tsize'
                  the measured size during the largest snapshot
              'repr'
                  string representation of the object
  
          Note that sorts on size are in descending order (placing most memory
          consuming items first), whereas name, repr, and creation time searches
          are in ascending order (alphabetical).
  
          The function returns self to allow calling functions on the result::
  
              stats.sort_stats('size').reverse_order().print_stats()