Sort a basis set shell into a standard order

    If use_copy is True, the input shells are not modified.
771597
def sort_shell(shell, use_copy=True):
    

    if use_copy:
        shell = copy.deepcopy(shell)

    # Transpose of coefficients
    tmp_c = list(map(list, zip(*shell['coefficients'])))

    # For each primitive, find the index of the first nonzero coefficient
    nonzero_idx = [next((i for i, x in enumerate(c) if float(x) != 0.0), None) for c in tmp_c]

    # Zip together exponents and coeffs for sorting
    tmp = zip(shell['exponents'], tmp_c, nonzero_idx)

    # Sort by decreasing value of exponent
    tmp = sorted(tmp, key=lambda x: -float(x[0]))

    # Now (stable) sort by first non-zero coefficient
    tmp = sorted(tmp, key=lambda x: int(x[2]))

    # Unpack, and re-transpose the coefficients
    tmp_c = [x[1] for x in tmp]
    shell['exponents'] = [x[0] for x in tmp]

    # Now sort the columns of the coefficient by index of first nonzero coefficient
    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
1002516
def _radix_sort(L, i=0):
  
  if len(L) <= 1: 
    return L
  done_bucket = []
  buckets = [ [] for x in range(255) ]
  for s in L:
    if i >= len(s):
      done_bucket.append(s)
    else:
      buckets[ ord(s[i]) ].append(s)
  buckets = [ _radix_sort(b, i + 1) for b in buckets ]
  return done_bucket + [ b for blist in buckets for b in blist ]
1242713
def reorder_indices(self, indices_order):
        ''
        # allow mixed index syntax like int
        indices_order, single = convert_index_to_keys(self.indices, indices_order)
        old_indices = force_list(self.indices.keys())

        if indices_order == old_indices: # no changes
            return

        if set(old_indices) != set(indices_order):
            raise KeyError('Keys in the new order do not match existing keys')

#        if len(old_indices) == 0: # already return since indices_order must equal to old_indices
#            return

        # must have more than 1 index to reorder
        new_idx = [old_indices.index(i) for i in indices_order]
        # reorder items
        items = [map(i.__getitem__, new_idx) for i in self.items()]
        self.clear(True)
        _MI_init(self, items, indices_order)
430933
def _sort_r(sorted, processed, key, deps, dependency_tree):
    """"""
    if key in processed:
        return
    processed.add(key)
    for dep_key in deps:
        dep_deps = dependency_tree.get(dep_key)
        if dep_deps is None:
            log.debug('"%s" not found, skipped', Repr(dep_key))
            continue
        _sort_r(sorted, processed, dep_key, dep_deps, dependency_tree)
    sorted.append((key, deps))
584993
def _update_sorting(self):
        
        key = self.key
        sorted_tops = self.sorted_tops
        tops = self.tops
        iterators = self.iterators
        for idx in self.idxs:
            try:
                tops[idx] = next(iterators[idx])
                top_key = key(tops[idx])
                if top_key not in sorted_tops:
                    sorted_tops[top_key] = []
                sorted_tops[top_key].append(idx)
            except StopIteration:
                pass
        if len(sorted_tops) == 0:
            raise StopIteration
        key, self.idxs = sorted_tops.popitem(last=False)
        self.c_idx = 0
748965
private void rotateTwiceOrdered() {
		Point2D_F64 a = ordered[0];
		Point2D_F64 b = ordered[1];
		Point2D_F64 c = ordered[2];
		Point2D_F64 d = ordered[3];

		ordered[0] = c;
		ordered[1] = d;
		ordered[2] = a;
		ordered[3] = b;
	}
679216
def prune_basis(basis, use_copy=True):
    

    if use_copy:
        basis = copy.deepcopy(basis)

    for k, el in basis['elements'].items():
        if not 'electron_shells' in el:
            continue

        shells = el.pop('electron_shells')
        shells = [prune_shell(sh, False) for sh in shells]

        # Remove any duplicates
        el['electron_shells'] = []

        for sh in shells:
            if sh not in el['electron_shells']:
                el['electron_shells'].append(sh)

    return basis
872102
def order_manually(sub_commands):
    """"""
    order = [
        "start",
        "projects",
    ]
    ordered = []
    commands = dict(zip([cmd for cmd in sub_commands], sub_commands))
    for k in order:
        ordered.append(commands.get(k, ""))
        if k in commands:
            del commands[k]

    # Add commands not present in `order` above
    for k in commands:
        ordered.append(commands[k])

    return ordered
986169
def _sort(self):
        
        self.versions = OrderedDict(sorted(self.versions.items(), key=lambda v: v[0]))
813050
def sort(iterable):
    
    ips = sorted(normalize_ip(ip) for ip in iterable)
    return [clean_ip(ip) for ip in ips]
1126624
final void normalizeRowOrder() {
    Collections.sort(rows, (a,b) -> Integer.compare(a.hashCode(), b.hashCode()));    
  }
910209
def _sort(self, session_groups):
    """"""

    # Sort by session_group name so we have a deterministic order.
    session_groups.sort(key=operator.attrgetter('name'))
    # Sort by lexicographical order of the _request.col_params whose order
    # is not ORDER_UNSPECIFIED. The first such column is the primary sorting
    # key, the second is the secondary sorting key, etc. To achieve that we
    # need to iterate on these columns in reverse order (thus the primary key
    # is the key used in the last sort).
    for col_param, extractor in reversed(list(zip(self._request.col_params,
                                                  self._extractors))):
      if col_param.order == api_pb2.ORDER_UNSPECIFIED:
        continue
      if col_param.order == api_pb2.ORDER_ASC:
        session_groups.sort(
            key=_create_key_func(
                extractor,
                none_is_largest=not col_param.missing_values_first))
      elif col_param.order == api_pb2.ORDER_DESC:
        session_groups.sort(
            key=_create_key_func(
                extractor,
                none_is_largest=col_param.missing_values_first),
            reverse=True)
      else:
        raise error.HParamsError('Unknown col_param.order given: %s' %
                                 col_param)
866591
def sort_matches(matches, basename)
        extname = File.extname(basename)
        aliases = @reverse_aliases[extname] || []

        matches.sort_by do |match|
          extnames = match.sub(basename, '').scan(/\.[^.]+/)
          extnames.inject(0) do |sum, ext|
            if i = extensions.index(ext)
              sum + i + 1
            elsif i = aliases.index(ext)
              sum + i + 11
            else
              sum
            end
          end
        end
      end
996785
def _reorder(string, salt):
    """"""
    len_salt = len(salt)

    if len_salt != 0:
        string = list(string)
        index, integer_sum = 0, 0
        for i in range(len(string) - 1, 0, -1):
            integer = ord(salt[index])
            integer_sum += integer
            j = (integer + index + integer_sum) % i
            string[i], string[j] = string[j], string[i]
            index = (index + 1) % len_salt
        string = ''.join(string)

    return string
1017235
def order_by_precedence(media_type_lst):
    
    ret = [set(), set(), set(), set()]
    for media_type in media_type_lst:
        precedence = _MediaType(media_type).precedence
        ret[3 - precedence].add(media_type)
    return [media_types for media_types in ret if media_types]
861103
def getInputOrder(ast, input_order=None):
    
    variables = {}
    for a in ast.allOf('variable'):
        variables[a.value] = a
    variable_names = set(variables.keys())

    if input_order:
        if variable_names != set(input_order):
            raise ValueError(
                "input names (%s) don't match those found in expression (%s)"
                % (input_order, variable_names))

        ordered_names = input_order
    else:
        ordered_names = list(variable_names)
        ordered_names.sort()
    ordered_variables = [variables[v] for v in ordered_names]
    return ordered_variables
779651
def sort_by_successors(l, succsOf):
    
    rlut = dict()
    nret = 0
    todo = list()
    for i in l:
        rlut[i] = set()
    for i in l:
        for j in succsOf(i):
            rlut[j].add(i)
    for i in l:
        if len(rlut[i]) == 0:
            todo.append(i)
    while len(todo) > 0:
        i = todo.pop()
        nret += 1
        yield i
        for j in succsOf(i):
            rlut[j].remove(i)
            if len(rlut[j]) == 0:
                todo.append(j)
    if nret != len(l):
        raise ValueError("Cycle detected")