Nodejs Utililty Methods Array Sort

List of utility methods to do Array Sort

Description

The list of methods to do Array Sort are organized into topic(s).

Method

propSortpropSort(prop)
Array.prototype.propSort = function propSort(prop) {
  return this.sort(function(a, b) { return +a[prop] - +b[prop]; });
};
Array.prototype.propAsort = function propSort(prop) {
  return this.sort(function(a, b) { return +b[prop] - +a[prop]; });
};
sleepSort()
Array.prototype.sleepSort = function () {
  var _arr = [];
  this.forEach(function (number) {
    setTimeout(function () {
      _arr.push(number);
    }, 2 * number);
  });
  return _arr;
stableSort(f)
Array.prototype.stableSort = function(f) {
  return this.map((ele, i) => ({ele: ele, i: i}))
  .sort((a, b) => {
    var val = f(a.ele, b.ele);
    if (val === 0) {
      return a.i - b.i;
    return val;
  }).map(ele => {
...
superSort()
Array.prototype.superSort = function() {
    function dynamicSort(property) {
        return function (obj1,obj2) {
            return obj1[property] > obj2[property] ? 1
                : obj1[property] < obj2[property] ? -1 : 0;
    var props = arguments;
    return this.sort(function (obj1, obj2) {
...
timeoutSort(f)
Array.prototype.timeoutSort = function (f) {
  this.forEach(function (n) {
    setTimeout(function () { f(n) }, 5 * n)
  });
validate_sorted()
Array.prototype.validate_sorted = function(){
  for (var i = 0; i < this.length - 1; i++){
    if (this[i] > this[i+1])
      return false;
  return true;
};
Array.prototype.qsort(left, right)
Array.prototype.qsort = function (left, right) {
  var pivot, newPivot, pivotVal, tmp;
  if (left === undefined) {
    left = 0;
    right = this.length - 1;
  if (left < right) {
    pivot = left + Math.ceil((right - left) / 2);
    pivotVal = this[pivot];
...