code: def sort_shell(shell, use_copy=True):
      if use_copy:
          shell = copy.deepcopy(shell)
      tmp_c = list(map(list, zip(*shell['coefficients'])))
      nonzero_idx = [next((i for i, x in enumerate(c) if float(x) != 0.0), None) for c in tmp_c]
      tmp = zip(shell['exponents'], tmp_c, nonzero_idx)
      tmp = sorted(tmp, key=lambda x: -float(x[0]))
      tmp = sorted(tmp, key=lambda x: int(x[2]))
      tmp_c = [x[1] for x in tmp]
      shell['exponents'] = [x[0] for x in tmp]
      tmp_c = list(map(list, zip(*tmp_c)))
      nonzero_idx = [next((i for i, x in enumerate(c) if float(x) != 0.0), None) for c in tmp_c]
      tmp = zip(tmp_c, nonzero_idx)
      tmp = sorted(tmp, key=lambda x: int(x[1]))
      tmp_c = [x[0] for x in tmp]
      shell['coefficients'] = tmp_c
      return shell