/*
This function is a replacement for usort that try to usort
but if items are equal then uses the relative position as second argument
338120
private function stableArraySort(array $input, callable $function): array
    {
        // create the item list with the item and the index
        $list = [];
        foreach (array_values($input) as $i => $value) {
            $item = new \stdClass();
            $item->item = $value;
            $item->index = $i;
            $list[] = $item;
        }

        // perform the usort, if comparison is equal then compare the index also
        usort($list, function (\stdClass $first, \stdClass $second) use ($function) {
            $value = $function($first->item, $second->item);
            if (0 === $value) {
                $value = $first->index <=> $second->index;
            }
            return $value;
        });

        // return only the items
        return array_column($list, 'item');
    }
867235
function sort(array, compare) {
  if (array.length <= 1) {
    return array;
  }

  var i;
  var middle = Math.floor(array.length / 2);
  var left = new Array(middle);
  var right = new Array(array.length - middle);

  for (i = 0; i < left.length; i++) {
    left[i] = array[i];
  }
  for (i = 0; i < right.length; i++) {
    right[i] = array[i + left.length];
  }

  return merge(sort(left, compare), sort(right, compare), compare);
}
1080959
function (a, b) {
      var opts = _.extend({sortOrder: 'asc', sortBy: 'index'}, options);

      a = a.data[opts.sortBy];
      b = b.data[opts.sortBy];

      var result = 0;
      result = a > b ? 1 : a < b ? -1 : 0;
      if(opts.sortOrder.toLowerCase() === 'desc') {
        return result * -1;
      }
      return result;
    }
595224
private static void sort (double[] coords1, int length1,
                              double[] coords2, int length2, int[] array) {
        int temp;
        int length = length1 + length2;
        double x1, y1, x2, y2;

        for (int i = 1; i < length; i++) {
            if (array[i - 1] < length1) {
                x1 = coords1[2 * array[i - 1]];
                y1 = coords1[2 * array[i - 1] + 1];
            } else {
                x1 = coords2[2 * (array[i - 1] - length1)];
                y1 = coords2[2 * (array[i - 1] - length1) + 1];
            }
            if (array[i] < length1) {
                x2 = coords1[2 * array[i]];
                y2 = coords1[2 * array[i] + 1];
            } else {
                x2 = coords2[2 * (array[i] - length1)];
                y2 = coords2[2 * (array[i] - length1) + 1];
            }
            int j = i;
            while (j > 0 && compare(x1, y1, x2, y2) <= 0) {
                temp = array[j];
                array[j] = array[j - 1];
                array[j - 1] = temp;
                j--;
                if (j > 0) {
                    if (array[j - 1] < length1) {
                        x1 = coords1[2 * array[j - 1]];
                        y1 = coords1[2 * array[j - 1] + 1];
                    } else {
                        x1 = coords2[2 * (array[j - 1] - length1)];
                        y1 = coords2[2 * (array[j - 1] - length1) + 1];
                    }
                    if (array[j] < length1) {
                        x2 = coords1[2 * array[j]];
                        y2 = coords1[2 * array[j] + 1];
                    } else {
                        x2 = coords2[2 * (array[j] - length1)];
                        y2 = coords2[2 * (array[j] - length1) + 1];
                    }
                }
            }
        }
    }
213654
function sort(recursive) {
         // do nothing if there is already a pending sort
         if (!this.pendingSort) {
           if (recursive === true) {
             // trigger other child container sort function (if any)
             for (var i = this.children.length, obj; i--, obj = this.children[i];) {
               if (obj instanceof me.Container) {
                 // note : this will generate one defered sorting function
                 // for each existing containe
                 obj.sort(recursive);
               }
             }
           }
           /** @ignore */


           this.pendingSort = me.utils.function.defer(function (self) {
             // sort everything in this container
             self.children.sort(self["_sort" + self.sortOn.toUpperCase()]); // clear the defer id

             self.pendingSort = null; // make sure we redraw everything

             me.game.repaint();
           }, this, this);
         }
       }
1193190
function _in_list(in_item, list, comparator){

	var retval = false;
	for(var li = 0; li < list.length; li++ ){
	    var list_item = list[li];

	    if( comparator ){
		var comp_op = comparator(in_item, list_item);
		if( comp_op && comp_op == true ){
		    retval = true;
		}
	    }else{
		if( in_item == list_item ){
		    retval = true;
		}
	    }
	}

	return retval;
    }
104510
function sorter (a, b) {
      const aDist = a.distance == null ? -1 : a.distance
      const bDist = b.distance == null ? -1 : b.distance
      if (aDist < bDist) {
        return 1
      }
      if (bDist < aDist) {
        return -1
      }
      return 0
    }
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);
            }
        }
    }
402199
function (item, itemIndex, then, complete, arrayWeTraverse) {
            $assert (item === data[itemIndex])
            $assert (itemIndex === currentIndex++)
            $assert (arrayWeTraverse === data)

            $assert (_.isFunction (then))
            $assert (_.isFunction (complete))

            then () }
21019
def sort_items_according_to_current_direction
      case @direction
      when nil
        @items = items.shift(2) + items.partition(&:directory?).flat_map(&:sort)
      when 'r'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort.reverse}
      when 'S', 's'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort_by {|i| -i.size}}
      when 'Sr', 'sr'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort_by(&:size)}
      when 't'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort {|x, y| y.mtime <=> x.mtime}}
      when 'tr'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort_by(&:mtime)}
      when 'c'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort {|x, y| y.ctime <=> x.ctime}}
      when 'cr'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort_by(&:ctime)}
      when 'u'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort {|x, y| y.atime <=> x.atime}}
      when 'ur'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort_by(&:atime)}
      when 'e'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort {|x, y| y.extname <=> x.extname}}
      when 'er'
        @items = items.shift(2) + items.partition(&:directory?).flat_map {|arr| arr.sort_by(&:extname)}
      end
      items.each.with_index {|item, index| item.index = index}
    end
189906
function (item) {
        item.value.sort(function (a, b) {
            if (a.key < b.key) {
                return -1;
            }
            /* istanbul ignore next */
            // Below line exists only for the sake of completeness. Generally the array will
            // always be sorted, and therefore it is impossible to get to this state in a test.
            return a.key > b.key ? 1 : 0;
        });
        return item;
    }
1031201
function defSortFn (a, b) {
    const a1 = `${a}`;
    const b1 = `${b}`;
    if (a1 < b1) {
        return -1;
    }
    if (a1 > b1) {
        return 1;
    }
    return 0;
}
581003
@Override
    protected final List<Description> orderItems(Collection<Description> descriptions) {
        /*
         * In practice, we will never get here--Sorters do their work in the
         * compare() method--but the Liskov substitution principle demands that
         * we obey the general contract of Orderable. Luckily, it's trivial to
         * implement.
         */
        List<Description> sorted = new ArrayList<Description>(descriptions);
        Collections.sort(sorted, this); // Note: it would be incorrect to pass in "comparator"
        return sorted;
    }
1004633
public function sort()
    {
        $index      = 0;
        $comparator = [$this->sorter, 'sort'];

        // add an index to provide stable sorting
        foreach ($this->items as &$item) {
            $item = [$index++, $item];
        }
        unset($item);

        uasort($this->items, function ($a, $b) use ($comparator) {
            $result = $comparator($a[1], $b[1]);

            // use the index to prevent undefined behaviour when comparator reports items are "equal"
            return $result === 0 ? $a[0] - $b[0] : $result;
        });

        // remove the index
        foreach ($this->items as &$item) {
            $item = $item[1];
        }
        unset($item);
    }
1110611
function insertSorted(arr, item, comparator) {
  var idx = binarySearch(arr, item, comparator);
  arr.splice(idx, 0, item);
}
343118
function sortByLabel(a, b) {
    if (a.sortval > b.sortval) return -1;
    else if (a.sortval == b.sortval) return 0;
    else return 1;
  }
95667
function argSort(vectors, ascending) {
  if (vectors.length !== ascending.length) {
    throw new Error('length of "ascending" must match the number of ' +
      'sort columns');
  }
  if (vectors.length === 0) {
    return null;
  }
  var nRow = vectors[0].size();
  var result = allocArray(nRow);
  for (var i = 0; i < nRow; i++) {
    result[i] = i;
  }

  // Create composite compare function
  var compareFuncs = ascending.map(function(asc) {
    return asc ? compare : compareReverse;
  });
  var compositeCompare = function(a, b) {
    var result = 0;
    for (var i = 0; i < compareFuncs.length; i++) {
      var aValue = vectors[i].values[a];
      var bValue = vectors[i].values[b];
      result = compareFuncs[i](aValue, bValue);
      if (result !== 0) {
        return result;
      }
    }
    // Order based on row index as last resort to ensure stable sorting
    return compare(a, b);
  };

  result.sort(compositeCompare);
  return result;
}