You can create one for yourself like the one below.
Syntax for using this
let numbSet = [44,55,22,55,44,11];
Ascending by default numbSet.sorty();
Descending numbSet.sorty('dsc');
If an array of objects then pass the key like
let objSet = [{a:44},{a:55},{a:22},{a:55},{a:44},{a:11}];
objSet.sorty('asc', 'a');
let numbSet = [44, 55, 22, 55, 44, 11];
let objSet = [{
a: 44
}, {
a: 55
}, {
a: 22
}, {
a: 55
}, {
a: 44
}, {
a: 11
}];
Array.prototype.sorty = function(type, key) {
if(this.length) {
if (type === 'dsc') {
return recuDsc(this, this.length - 1, key ? key : false);
} else {
// default assending
return recuAsc(this, 0, key ? key : false);
}
}
return this;
}
function recuAsc(arry, indx, key) {
let arryLength = arry.length;
let isSmaller = false;
let a, b, i = indx + 1;
if (indx != (arryLength - 1)) {
for (i; i < arryLength; i++) {
a = arry[indx];
b = arry[i];
isSmaller = key ? a[key] < b[key] : a < b;
if (!isSmaller) {
arry[indx] = b;
arry[i] = a;
}
}
recuAsc(arry, indx + 1, key ? key : false);
}
return arry;
}
function recuDsc(arry, indx, key) {
let arryLength = arry.length;
let isSmaller = false;
let a, b, i = indx - 1;
if (indx != (0)) {
for (i; i >= 0; i--) {
a = arry[indx];
b = arry[i]
isSmaller = key ? a[key] < b[key] : a < b;
if (!isSmaller) {
arry[indx] = b;
arry[i] = a;
}
}
recuDsc(arry, indx - 1, key ? key : false);
}
return arry;
}
console.log('Sorty an Array of numbers, ascending');
console.log(numbSet.sorty());
console.log('Sorty an Array of numbers, descending');
console.log(numbSet.sorty('dsc'));
console.log('Sorty an Array of Object');
console.log(objSet.sorty('asc', 'a'))