A comparator to sort bboxes from left to right, top to bottom
961440
def xy_reading_order(e1, e2):
    
    b1 = e1.bbox
    b2 = e2.bbox
    if round(b1[x0]) == round(b2[x0]):
        return float_cmp(b1[y0], b2[y0])
    return float_cmp(b1[x0], b2[x0])
1043614
protected void zSort(List<? extends SpatialComparable> objs, int start, int end, double[] mms, int[] dims, int depth) {
    final int numdim = (dims != null) ? dims.length : (mms.length >> 1);
    final int edim = (dims != null) ? dims[depth] : depth;
    // Find the splitting points.
    final double min = mms[2 * edim], max = mms[2 * edim + 1];
    double spos = (min + max) / 2.;
    // Safeguard against duplicate points:
    if(max - spos < STOPVAL || spos - min < STOPVAL) {
      boolean ok = false;
      for(int d = 0; d < numdim; d++) {
        int d2 = ((dims != null) ? dims[d] : d) << 1;
        if(mms[d2 + 1] - mms[d2] >= STOPVAL) {
          ok = true;
          break;
        }
      }
      if(!ok) {
        return;
      }
    }
    int split = pivotizeList1D(objs, start, end, edim, spos, false);
    assert (start <= split && split <= end);
    int nextdim = (depth + 1) % numdim;
    if(start < split - 1) {
      mms[2 * edim] = min;
      mms[2 * edim + 1] = spos;
      zSort(objs, start, split, mms, dims, nextdim);
    }
    if(split < end - 1) {
      mms[2 * edim] = spos;
      mms[2 * edim + 1] = max;
      zSort(objs, split, end, mms, dims, nextdim);
    }
    // Restore ranges
    mms[2 * edim] = min;
    mms[2 * edim + 1] = max;
  }
123960
def sort(self, values):
        """"""
        for level in self:
            for wire1, wire2 in level:
                if values[wire1] > values[wire2]:
                    values[wire1], values[wire2] = values[wire2], values[wire1]
175053
public static void sort(double[] x, int start, int end)
    {
        int a = start;
        int n = end-start;
        if (n < 7)/* Insertion sort on smallest arrays */
        {
            insertionSort(x, a, end);
            return;
        }
        
        int pm = a + (n/2);  /* Small arrays, middle element */
        if (n > 7)
        {
            int pl = a;
            int pn = a + n - 1;
            if (n > 40) /* Big arrays, pseudomedian of 9 */
            {        
                int s = n / 8;
                pl = med3(x, pl, pl + s, pl + 2 * s);
                pm = med3(x, pm - s, pm, pm + s);
                pn = med3(x, pn - 2 * s, pn - s, pn);
            }
            pm = med3(x, pl, pm, pn); /* Mid-size, med of 3 */
        }
        double pivotValue = x[pm];

        int pa = a, pb = pa, pc = end - 1, pd = pc;
        while (true)
        {
            while (pb <= pc && x[pb] <= pivotValue)
            {
                if (x[pb] == pivotValue)
                    swap(x, pa++, pb);
                pb++;
            }
            while (pc >= pb && x[pc] >= pivotValue)
            {
                if (x[pc] == pivotValue)
                    swap(x, pc, pd--);
                pc--;
            }
            if (pb > pc)
                break;
            swap(x, pb++, pc--);
        }

        
        int s;
        int pn = end;
        s = Math.min(pa - a, pb - pa);
        vecswap(x, a, pb - s, s);
        s = Math.min(pd - pc, pn - pd - 1);
        vecswap(x, pb, pn - s, s);

        //recurse 
        if ((s = pb - pa) > 1)
            sort(x, a, a+s);
        if ((s = pd - pc) > 1)
            sort(x, pn - s, pn);
        
    }
138260
def sort(m, reverse=False):
    """"""
    ty = type(m)
    if ty == matrix:
        m = list(m)
    m = sorted(m, reverse=reverse)
    if ty == matrix:
        m = matrix(m)
    return m
1072788
private static void quickSort(double[] keys, int[] vals, final int start, final int end) {
    final int len = end - start;
    if(len < INSERTION_THRESHOLD) {
      insertionSort(keys, vals, start, end);
      return;
    }
    final int last = end - 1;

    // Choose pivots by looking at five candidates.
    final int seventh = (len >> 3) + (len >> 6) + 1;
    final int m3 = (start + end) >> 1; // middle
    final int m2 = m3 - seventh;
    final int m1 = m2 - seventh;
    final int m4 = m3 + seventh;
    final int m5 = m4 + seventh;

    // Mixture of insertion and merge sort:
    sort5(keys, vals, m1, m2, m3, m4, m5);

    // Move pivot to the front.
    double pivotkey = keys[m3];
    int pivotval = vals[m3];
    keys[m3] = keys[start];
    vals[m3] = vals[start];

    // The interval to pivotize
    int left = start + 1; // Without pivot
    int right = last; // inclusive

    // This is the classic QuickSort loop:
    while(true) {
      // Move duplicates to right partition, i.e. < here, <= below.
      while(left <= right && keys[left] < pivotkey) {
        left++;
      }
      while(left <= right && pivotkey <= keys[right]) {
        right--;
      }
      if(right <= left) {
        break;
      }
      swap(keys, vals, left, right);
      left++;
      right--;
    }
    // right now points to the last element smaller than the pivot.
    // Move pivot back into the appropriate place
    keys[start] = keys[right];
    vals[start] = vals[right];
    keys[right] = pivotkey;
    vals[right] = pivotval;

    // Recursion when more than one element only:
    if(start + 1 < right) {
      quickSort(keys, vals, start, right);
    }
    int rstart = right + 1;
    // Avoid recursing on duplicates of the pivot:
    while(rstart < last && keys[rstart] <= keys[right]) {
      rstart++;
    }
    // Recurse when _more_ than 1 element only
    if(rstart < last) {
      quickSort(keys, vals, rstart, end);
    }
  }
570011
def _sort_kw_matches(skw_matches, limit=0):
    """"""
    sorted_keywords = list(skw_matches.items())
    sorted(sorted_keywords, key=cmp_to_key(_skw_matches_comparator))
    return limit and sorted_keywords[:limit] or sorted_keywords
648320
def items_sort(cls, items):
        
        class t(tuple):
            """Tuple with element-based sorting"""
            def __cmp__(self, other):
                for a, b in six.moves.zip_longest(self, other):
                    if a != b:
                        if a is None:
                            return 1
                        if b is None:
                            return -1
                        return a - b
                return 0

            def __lt__(self, other):
                return self.__cmp__(other) < 0
            def __gt_(self, other):
                return self.__cmp__(other) > 0
            def __le__(self, other):
                return self.__cmp__(other) <= 0
            def __ge_(self, other):
                return self.__cmp__(other) >= 0
            def __eq__(self, other):
                return self.__cmp__(other) == 0
            def __ne__(self, other):
                return self.__cmp__(other) != 0

        def key_func(x):
            if x.indented:
                return t((int(x.parent_item.sort), int(x.sort)))
            return t((int(x.sort), ))

        return sorted(items, key=key_func, reverse=True)
678194
public Table sortOn(Comparator<Row> rowComparator) {
        Row row1 = new Row(this);
        Row row2 = new Row(this);
        return sortOn((IntComparator) (k1, k2) -> {
            row1.at(k1);
            row2.at(k2);
            return rowComparator.compare(row1, row2);
        });
    }
257036
def sort_dicoms(dicoms):
    
    # find most significant axis to use during sorting
    # the original way of sorting (first x than y than z) does not work in certain border situations
    # where for exampe the X will only slightly change causing the values to remain equal on multiple slices
    # messing up the sorting completely)
    dicom_input_sorted_x = sorted(dicoms, key=lambda x: (x.ImagePositionPatient[0]))
    dicom_input_sorted_y = sorted(dicoms, key=lambda x: (x.ImagePositionPatient[1]))
    dicom_input_sorted_z = sorted(dicoms, key=lambda x: (x.ImagePositionPatient[2]))
    diff_x = abs(dicom_input_sorted_x[-1].ImagePositionPatient[0] - dicom_input_sorted_x[0].ImagePositionPatient[0])
    diff_y = abs(dicom_input_sorted_y[-1].ImagePositionPatient[1] - dicom_input_sorted_y[0].ImagePositionPatient[1])
    diff_z = abs(dicom_input_sorted_z[-1].ImagePositionPatient[2] - dicom_input_sorted_z[0].ImagePositionPatient[2])
    if diff_x >= diff_y and diff_x >= diff_z:
        return dicom_input_sorted_x
    if diff_y >= diff_x and diff_y >= diff_z:
        return dicom_input_sorted_y
    if diff_z >= diff_x and diff_z >= diff_y:
        return dicom_input_sorted_z
1139058
function binarySearch(arr, item, comparator) {
  var low = 0;
  var high = arr.length;
  var mid;
  while (low < high) {
    mid = (low + high) >>> 1;
    if (comparator(arr[mid], item) < 0) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  return low;
}
514697
private static <V> Collection<V> sorted(Iterable<V> source, Comparator<V> comparator, int size)
    {
        V[] vs = (V[]) new Object[size];
        int i = 0;
        for (V v : source)
            vs[i++] = v;
        Arrays.sort(vs, comparator);
        return Arrays.asList(vs);
    }
671687
private void selectionSort(final T[] array, int start, int end, Comparator<T> lt) {
    int i;
    int j;
    int bestI;
    T tmp;
    for (i = start; i < end; i++) {
      bestI = i;
      for (j = i + 1; j < end; j++) {
        if (lt.compare(array[j], array[bestI]) < 0)
          bestI = j;
      }
      tmp = array[i];
      array[i] = array[bestI];
      array[bestI] = tmp;
    }
  }
1052729
private void sortLists(){
		//sort RGBformats
		//if native RGB24 is supported, put it first
		moveToFirstIfNative(RGBformats, V4L4JConstants.IMF_RGB24);
		
		//sort BGRformats
		//if native BGR24 is supported, put it first
		moveToFirstIfNative(BGRformats, V4L4JConstants.IMF_BGR24);
		
		//sort YUV420formats
		//if native YUV420 is supported, put it first
		moveToFirstIfNative(YUV420formats, V4L4JConstants.IMF_YUV420);
		
		//sort YVU420formats
		//if native YVU420 is supported, put it first
		moveToFirstIfNative(YVU420formats, V4L4JConstants.IMF_YVU420);
		
		//sort JPEGformats
		//put native formats first and libvideo converted ones next
		moveNativeFirst(JPEGformats);
		//if native RGB32 is supported, put it first
		moveToFirstIfNative(JPEGformats, V4L4JConstants.IMF_RGB32);
		//if native RGB24 is supported, put it first
		moveToFirstIfNative(JPEGformats, V4L4JConstants.IMF_RGB24);
		//if native UYVY is supported, put it first
		moveToFirstIfNative(JPEGformats, V4L4JConstants.IMF_UYVY);
		//if native YUYV is supported, put it first
		moveToFirstIfNative(JPEGformats, V4L4JConstants.IMF_YUYV);
		//if native YUV420P is supported, put it first
		moveToFirstIfNative(JPEGformats, V4L4JConstants.IMF_YUV420);
		//if native MJPEG is supported, put it first
		moveToFirstIfNative(JPEGformats, V4L4JConstants.IMF_MJPEG);
		//if native JPEG is supported, put it first
		moveToFirstIfNative(JPEGformats, V4L4JConstants.IMF_JPEG);
	}
40600
def normalize_bbox(coords, ymax, scaler=2):
    
    return [
        coords[0] * scaler,
        ymax - (coords[3] * scaler),
        coords[2] * scaler,
        ymax - (coords[1] * scaler),
    ]
1259956
private static <T extends Comparable<? super T>> void sort(T[] a, int n) {
        int inc = 1;
        do {
            inc *= 3;
            inc++;
        } while (inc <= n);

        do {
            inc /= 3;
            for (int i = inc; i < n; i++) {
                T v = a[i];
                int j = i;
                while (a[j - inc].compareTo(v) < 0) {
                    a[j] = a[j - inc];
                    j -= inc;
                    if (j < inc) {
                        break;
                    }
                }
                a[j] = v;
            }
        } while (inc > 1);
    }
1188514
def _sorter(generated):
    """"""
    pairs = [(os.path.dirname(f), os.path.basename(f))
             for f in set(list(generated))]

    pairs.sort()

    return [os.path.join(pair[0], pair[1]) for pair in pairs]