Recursive topological sort implementation.
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))
426986
public static void sort(BaseComponent parent, boolean recurse) {
        if (parent == null || parent.getChildren().size() < 2) {
            return;
        }

        int i = 1;
        int size = parent.getChildren().size();

        while (i < size) {
            Treenode item1 = (Treenode) parent.getChildren().get(i - 1);
            Treenode item2 = (Treenode) parent.getChildren().get(i);

            if (compare(item1, item2) > 0) {
                parent.swapChildren(i - 1, i);
                i = i == 1 ? 2 : i - 1;
            } else {
                i++;
            }
        }

        if (recurse) {
            for (BaseComponent child : parent.getChildren()) {
                sort(child, recurse);
            }
        }
    }
687410
public static void sort(int[] arr, int[] brr, int n) {
        int jstack = -1;
        int l = 0;
        int[] istack = new int[NSTACK];
        int ir = n - 1;

        int i, j, k, a, b;
        for (;;) {
            if (ir - l < M) {
                for (j = l + 1; j <= ir; j++) {
                    a = arr[j];
                    b = brr[j];
                    for (i = j - 1; i >= l; i--) {
                        if (arr[i] <= a) {
                            break;
                        }
                        arr[i + 1] = arr[i];
                        brr[i + 1] = brr[i];
                    }
                    arr[i + 1] = a;
                    brr[i + 1] = b;
                }
                if (jstack < 0) {
                    break;
                }
                ir = istack[jstack--];
                l = istack[jstack--];
            } else {
                k = (l + ir) >> 1;
                SortUtils.swap(arr, k, l + 1);
                SortUtils.swap(brr, k, l + 1);
                if (arr[l] > arr[ir]) {
                    SortUtils.swap(arr, l, ir);
                    SortUtils.swap(brr, l, ir);
                }
                if (arr[l + 1] > arr[ir]) {
                    SortUtils.swap(arr, l + 1, ir);
                    SortUtils.swap(brr, l + 1, ir);
                }
                if (arr[l] > arr[l + 1]) {
                    SortUtils.swap(arr, l, l + 1);
                    SortUtils.swap(brr, l, l + 1);
                }
                i = l + 1;
                j = ir;
                a = arr[l + 1];
                b = brr[l + 1];
                for (;;) {
                    do {
                        i++;
                    } while (arr[i] < a);
                    do {
                        j--;
                    } while (arr[j] > a);
                    if (j < i) {
                        break;
                    }
                    SortUtils.swap(arr, i, j);
                    SortUtils.swap(brr, i, j);
                }
                arr[l + 1] = arr[j];
                arr[j] = a;
                brr[l + 1] = brr[j];
                brr[j] = b;
                jstack += 2;

                if (jstack >= NSTACK) {
                    throw new IllegalStateException("NSTACK too small in sort.");
                }

                if (ir - i + 1 >= j - l) {
                    istack[jstack] = ir;
                    istack[jstack - 1] = i;
                    ir = j - 1;
                } else {
                    istack[jstack] = j - 1;
                    istack[jstack - 1] = l;
                    l = i;
                }
            }
        }
    }
1352562
private void ssMintroSort(int PA, int first, int last, int depth) {
        final int STACK_SIZE = SS_MISORT_STACKSIZE;
        StackElement[] stack = new StackElement[STACK_SIZE];
        int Td;// T ptr
        int a, b, c, d, e, f;// SA ptr
        int s, t;
        int ssize;
        int limit;
        int v, x = 0;
        for (ssize = 0, limit = ssIlg(last - first); ; ) {

            if ((last - first) <= SS_INSERTIONSORT_THRESHOLD) {
                if (1 < (last - first)) {
                    ssInsertionSort(PA, first, last, depth);
                }
                if (ssize > 0) {
                    StackElement se = stack[--ssize];
                    first = se.a;
                    last = se.b;
                    depth = se.c;
                    limit = se.d;
                } else {
                    return;
                }

                continue;
            }

            Td = depth;
            if (limit-- == 0) {
                ssHeapSort(Td, PA, first, last - first);

            }
            if (limit < 0) {
                for (a = first + 1, v = T[start + Td + SA[PA + SA[first]]]; a < last; ++a) {
                    if ((x = T[start + Td + SA[PA + SA[a]]]) != v) {
                        if (1 < (a - first)) {
                            break;
                        }
                        v = x;
                        first = a;
                    }
                }

                if (T[start + Td + SA[PA + SA[first]] - 1] < v) {
                    first = ssPartition(PA, first, a, depth);
                }
                if ((a - first) <= (last - a)) {
                    if (1 < (a - first)) {
                        stack[ssize++] = new StackElement(a, last, depth, -1);
                        last = a;
                        depth += 1;
                        limit = ssIlg(a - first);
                    } else {
                        first = a;
                        limit = -1;
                    }
                } else {
                    if (1 < (last - a)) {
                        stack[ssize++] = new StackElement(first, a, depth + 1, ssIlg(a
                            - first));
                        first = a;
                        limit = -1;
                    } else {
                        last = a;
                        depth += 1;
                        limit = ssIlg(a - first);
                    }
                }
                continue;
            }

            // choose pivot
            a = ssPivot(Td, PA, first, last);
            v = T[start + Td + SA[PA + SA[a]]];
            swapInSA(first, a);

            // partition
            for (b = first; (++b < last) && ((x = T[start + Td + SA[PA + SA[b]]]) == v); ) {
            }
            if (((a = b) < last) && (x < v)) {
                for (; (++b < last) && ((x = T[start + Td + SA[PA + SA[b]]]) <= v); ) {
                    if (x == v) {
                        swapInSA(b, a);
                        ++a;
                    }
                }
            }

            for (c = last; (b < --c) && ((x = T[start + Td + SA[PA + SA[c]]]) == v); ) {
            }
            if ((b < (d = c)) && (x > v)) {
                for (; (b < --c) && ((x = T[start + Td + SA[PA + SA[c]]]) >= v); ) {
                    if (x == v) {
                        swapInSA(c, d);
                        --d;
                    }
                }
            }

            for (; b < c; ) {
                swapInSA(b, c);
                for (; (++b < c) && ((x = T[start + Td + SA[PA + SA[b]]]) <= v); ) {
                    if (x == v) {
                        swapInSA(b, a);
                        ++a;
                    }
                }
                for (; (b < --c) && ((x = T[start + Td + SA[PA + SA[c]]]) >= v); ) {
                    if (x == v) {
                        swapInSA(c, d);
                        --d;
                    }
                }
            }

            if (a <= d) {
                c = b - 1;

                if ((s = a - first) > (t = b - a)) {
                    s = t;
                }
                for (e = first, f = b - s; 0 < s; --s, ++e, ++f) {
                    swapInSA(e, f);
                }
                if ((s = d - c) > (t = last - d - 1)) {
                    s = t;
                }
                for (e = b, f = last - s; 0 < s; --s, ++e, ++f) {
                    swapInSA(e, f);
                }

                a = first + (b - a);
                c = last - (d - c);
                b = (v <= T[start + Td + SA[PA + SA[a]] - 1]) ? a : ssPartition(PA, a, c,
                    depth);

                if ((a - first) <= (last - c)) {
                    if ((last - c) <= (c - b)) {
                        stack[ssize++] = new StackElement(b, c, depth + 1, ssIlg(c - b));
                        stack[ssize++] = new StackElement(c, last, depth, limit);
                        last = a;
                    } else if ((a - first) <= (c - b)) {
                        stack[ssize++] = new StackElement(c, last, depth, limit);
                        stack[ssize++] = new StackElement(b, c, depth + 1, ssIlg(c - b));
                        last = a;
                    } else {
                        stack[ssize++] = new StackElement(c, last, depth, limit);
                        stack[ssize++] = new StackElement(first, a, depth, limit);
                        first = b;
                        last = c;
                        depth += 1;
                        limit = ssIlg(c - b);
                    }
                } else {
                    if ((a - first) <= (c - b)) {
                        stack[ssize++] = new StackElement(b, c, depth + 1, ssIlg(c - b));
                        stack[ssize++] = new StackElement(first, a, depth, limit);
                        first = c;
                    } else if ((last - c) <= (c - b)) {
                        stack[ssize++] = new StackElement(first, a, depth, limit);
                        stack[ssize++] = new StackElement(b, c, depth + 1, ssIlg(c - b));
                        first = c;
                    } else {
                        stack[ssize++] = new StackElement(first, a, depth, limit);
                        stack[ssize++] = new StackElement(c, last, depth, limit);
                        first = b;
                        last = c;
                        depth += 1;
                        limit = ssIlg(c - b);
                    }
                }

            } else {
                limit += 1;
                if (T[start + Td + SA[PA + SA[first]] - 1] < v) {
                    first = ssPartition(PA, first, last, depth);
                    limit = ssIlg(last - first);
                }
                depth += 1;
            }

        }

    }
1175347
def topological_sort(self):
        
        graph = self.graph

        in_degree = {}
        for u in graph:
            in_degree[u] = 0

        for u in graph:
            for v in graph[u]:
                in_degree[v] += 1

        queue = deque()
        for u in in_degree:
            if in_degree[u] == 0:
                queue.appendleft(u)

        sorted_graph = []
        while queue:
            u = queue.pop()
            sorted_graph.append(u)
            for v in sorted(graph[u]):
                in_degree[v] -= 1
                if in_degree[v] == 0:
                    queue.appendleft(v)

        if len(sorted_graph) == len(graph):
            return sorted_graph
        else:
            raise ValueError('graph is not acyclic')
123484
public static void sort(int n, int[] Xip, int Xip_offset, int[] Xlen, int Xlen_offset,
			double[] LU, int[] Tp, int[] Tj, double[] Tx, int[] W)
	{
		/*int[]*/double[] Xi ;
		double[] Xx ;
		int p, i, j, nz, tp, xlen, pend ;
		int[] len = new int[1] ;
		int[] Xi_offset = new int[1] ;
		int[] Xx_offset = new int[1] ;

		ASSERT (klu_valid_LU (n, FALSE, Xip, Xip_offset, Xlen, Xlen_offset, LU)) ;

		/* count the number of entries in each row of L or U */
		for (i = 0 ; i < n ; i++)
		{
			W [i] = 0 ;
		}
		for (j = 0 ; j < n ; j++)
		{
			Xi = Xx = GET_POINTER (LU, Xip, Xip_offset, Xlen, Xlen_offset, Xi_offset, Xx_offset, j, len) ;
			for (p = 0 ; p < len[0] ; p++)
			{
				W [(int) Xi [Xi_offset[0] + p]]++ ;
			}
		}

		/* construct the row pointers for T */
		nz = 0 ;
		for (i = 0 ; i < n ; i++)
		{
			Tp [i] = nz ;
			nz += W [i] ;
		}
		Tp [n] = nz ;
		for (i = 0 ; i < n ; i++)
		{
			W [i] = Tp [i] ;
		}

		/* transpose the matrix into Tp, Ti, Tx */
		for (j = 0 ; j < n ; j++)
		{
			Xi = Xx = GET_POINTER (LU, Xip, Xip_offset, Xlen, Xlen_offset, Xi_offset, Xx_offset, j, len) ;
			for (p = 0 ; p < len[0] ; p++)
			{
				tp = W [(int) Xi [Xi_offset[0] + p]]++ ;
				Tj [tp] = j ;
				Tx [tp] = Xx [Xx_offset[0] + p] ;
			}
		}

		/* transpose the matrix back into Xip, Xlen, Xi, Xx */
		for (j = 0 ; j < n ; j++)
		{
			W [j] = 0 ;
		}
		for (i = 0 ; i < n ; i++)
		{
			pend = Tp [i+1] ;
			for (p = Tp [i] ; p < pend ; p++)
			{
				j = Tj [p] ;
				Xi = Xx = GET_POINTER (LU, Xip, Xip_offset, Xlen, Xlen_offset, Xi_offset, Xx_offset, j, len) ;
				xlen = W [j]++ ;
				Xi [Xi_offset[0] + xlen] = i ;
				Xx [Xx_offset[0] + xlen] = Tx [p] ;
			}
		}

		ASSERT (klu_valid_LU (n, FALSE, Xip, Xip_offset, Xlen, Xlen_offset, LU)) ;
	}
1071582
function calcSortedIntersections(lines,y) {
    var xlist = [];
    for(var i=0; i<lines.length; i++) {
        var A = lines[i].start;
        var B = lines[i].end;
        if(A.y<y && B.y>=y || B.y<y && A.y>=y) {
            var xval = A.x + (y-A.y) / (B.y-A.y) * (B.x-A.x);
            xlist.push(xval);
        }
    }
    return xlist.sort(function(a,b) {  return a-b; });
}
561770
def sorted_index(values, x):
    
    i = bisect_left(values, x)
    j = bisect_right(values, x)
    return values[i:j].index(x) + i
861461
private static void quickSort1(int off, int len, IntComparator comp, Swapper swapper) {
	// Insertion sort on smallest arrays
	if (len < SMALL) {
		for (int i=off; i<len+off; i++)
		for (int j=i; j>off && (comp.compare(j-1,j)>0); j--) {
		    swapper.swap(j, j-1);
		}
		return;
	}

	// Choose a partition element, v
	int m = off + len/2;       // Small arrays, middle element
	if (len > SMALL) {
		int l = off;
		int n = off + len - 1;
		if (len > MEDIUM) {        // Big arrays, pseudomedian of 9
			int s = len/8;
			l = med3(l,     l+s, l+2*s, comp);
			m = med3(m-s,   m,   m+s, comp);
			n = med3(n-2*s, n-s, n, comp);
		}
		m = med3(l, m, n, comp); // Mid-size, med of 3
	}
	//long v = x[m];
	
	// Establish Invariant: v* (<v)* (>v)* v*
	int a = off, b = a, c = off + len - 1, d = c;
	while(true) {
		int comparison;
		while (b <= c && ((comparison=comp.compare(b,m))<=0)) {
			if (comparison == 0) {
				if (a==m) m = b; // moving target; DELTA to JDK !!!
				else if (b==m) m = a; // moving target; DELTA to JDK !!!
			    swapper.swap(a++, b);
			}
			b++;
		}
		while (c >= b && ((comparison=comp.compare(c,m))>=0)) {
			if (comparison == 0) {
				if (c==m) m = d; // moving target; DELTA to JDK !!!
				else if (d==m) m = c; // moving target; DELTA to JDK !!!
			    swapper.swap(c, d--);
			}
			c--;
		}
		if (b > c) break;
		if (b==m) m = d; // moving target; DELTA to JDK !!!
		else if (c==m) m = c; // moving target; DELTA to JDK !!!
		swapper.swap(b++, c--);
	}

	// Swap partition elements back to middle
	int s, n = off + len;
	s = java.lang.Math.min(a-off, b-a  );  vecswap(swapper, off, b-s, s);
	s = java.lang.Math.min(d-c,   n-d-1);  vecswap(swapper, b,   n-s, s);

	// Recursively sort non-partition-elements
	if ((s = b-a) > 1) 
		quickSort1(off, s, comp, swapper);
	if ((s = d-c) > 1) 
		quickSort1(n-s, s, comp, swapper);
}
901874
function sortRankedItems(a, b) {
  const aFirst = -1
  const bFirst = 1
  const {rank: aRank, index: aIndex, keyIndex: aKeyIndex} = a
  const {rank: bRank, index: bIndex, keyIndex: bKeyIndex} = b
  const same = aRank === bRank
  if (same) {
    if (aKeyIndex === bKeyIndex) {
      return aIndex < bIndex ? aFirst : bFirst
    } else {
      return aKeyIndex < bKeyIndex ? aFirst : bFirst
    }
  } else {
    return aRank > bRank ? aFirst : bFirst
  }
}
1339132
function indexCurve (data, start, minX, minY, size) {
  var node = start

  do {
    if (node.z === null) {
      node.z = zOrder(data[node.i], data[node.i + 1], minX, minY, size)
    }
    node.prevZ = node.prev
    node.nextZ = node.next
    node = node.next
  } while (node !== start)

  node.prevZ.nextZ = null
  node.prevZ = null

  sortLinked(node)
}
737858
def topological_order(graph):
    
    V = range(len(graph))
    indeg = [0 for _ in V]
    for node in V:            # compute indegree
        for neighbor in graph[node]:
            indeg[neighbor] += 1
    Q = [node for node in V if indeg[node] == 0]
    order = []
    while Q:
        node = Q.pop()        # node without incoming arrows
        order.append(node)
        for neighbor in graph[node]:
            indeg[neighbor] -= 1
            if indeg[neighbor] == 0:
                Q.append(neighbor)
    return order
555052
function sortTopologically (originalOrder, edgesById) {
  const sorted = []
  const marked = new Set()

  const visit = (mark) => {
    if (marked.has(mark)) return
    marked.add(mark)
    const edges = edgesById.get(mark)
    if (edges != null) {
      edges.forEach(visit)
    }
    sorted.push(mark)
  }

  originalOrder.forEach(visit)
  return sorted
}
169186
def sort_recursive(data):
    

    newdict = {}

    for i in data.items():
        if type(i[1]) is dict:
            newdict[i[0]] = sort_recursive(i[1])
        else:
            newdict[i[0]] = i[1]

    return OrderedDict(sorted(newdict.items(), key=lambda item: (compare_type(type(item[1])), item[0])))
84086
def sort_targets(targets):
  

  roots, inverted_deps = invert_dependencies(targets)
  ordered = []
  visited = set()

  def topological_sort(target):
    if target not in visited:
      visited.add(target)
      if target in inverted_deps:
        for dep in inverted_deps[target]:
          topological_sort(dep)
      ordered.append(target)

  for root in roots:
    topological_sort(root)

  return ordered
632556
def topologically_sorted_indexes(self):
        
        try:
            return self._topologcally_sorted_indexes_cache
        except AttributeError:
            seen = set()
            order = []
            explored = set()

            for i in self.edges:
                if i in explored:
                    continue
                fringe = [i]
                while fringe:
                    w = fringe[-1]  # depth first search
                    if w in explored:  # already looked down this branch
                        fringe.pop()
                        continue
                    seen.add(w)  # mark as seen
                    # Check successors for cycles and for new nodes
                    new_nodes = []
                    for n in self.outputs_of(w):
                        if n not in explored:
                            if n in seen:  # CYCLE !!
                                raise RuntimeError("Graph contains a cycle.")
                            new_nodes.append(n)
                    if new_nodes:  # Add new_nodes to fringe
                        fringe.extend(new_nodes)
                    else:  # No new nodes so w is fully explored
                        explored.add(w)
                        order.append(w)
                        fringe.pop()  # done considering this node
            self._topologcally_sorted_indexes_cache = tuple(filter(lambda i: type(i) is int, reversed(order)))
            return self._topologcally_sorted_indexes_cache
1028907
def _quasi_topological_sort(self):
        

        # Clear the existing sorting result
        self._quasi_topological_order = {}

        ctr = self._graph.number_of_nodes()

        for ep in self._entry_points:
            # FIXME: This is not always correct. We'd better store CFGNodes in self._entry_points
            ep_node = self.get_any_node(ep)

            if not ep_node:
                continue

            for n in networkx.dfs_postorder_nodes(self._graph, source=ep_node):
                if n not in self._quasi_topological_order:
                    self._quasi_topological_order[n] = ctr
                    ctr -= 1