code: def slice(self, *slice_args, **kwargs):
          sorted_by = kwargs.pop('sorted_by', 'energy')
          if kwargs:
              raise TypeError('slice got an unexpected '
                              'keyword argument {!r}'.format(kwargs.popitem()[0]))
          if slice_args:
              selector = slice(*slice_args)
          else:
              selector = slice(None)
          if sorted_by is None:
              record = self.record[selector]
          else:
              sort_indices = np.argsort(self.record[sorted_by])
              record = self.record[sort_indices[selector]]
          return type(self)(record, self.variables, copy.deepcopy(self.info),
                            self.vartype)