sorting an array in descending order in javascript without using any built-in method - Stack Overflow

admin2025-04-21  0

I need to sort a simple array in descending order in javaScript without using a built-in method and it took me way too much time already... please help

function dscSort(array) {
    for (var i = 1; i < array.length; i++) {
        var tmp = array[i];
        for (var j = i - 1; j >= 0 && (array[j] < array[i]); j--) {
            array[j + 1] = array[j];
        }
        array[j + 1] = tmp;
    }
    return array;
}

I need to sort a simple array in descending order in javaScript without using a built-in method and it took me way too much time already... please help

function dscSort(array) {
    for (var i = 1; i < array.length; i++) {
        var tmp = array[i];
        for (var j = i - 1; j >= 0 && (array[j] < array[i]); j--) {
            array[j + 1] = array[j];
        }
        array[j + 1] = tmp;
    }
    return array;
}
Share Improve this question edited Aug 29, 2019 at 23:34 Ranjan R.P. 1,1433 gold badges13 silver badges29 bronze badges asked Apr 14, 2017 at 21:36 MaryMary 713 silver badges6 bronze badges 5
  • What do you mean by "ready function" – inoabrian Commented Apr 14, 2017 at 21:41
  • like i cant use array.sort(), but have to write it like i just did, but its not working – Mary Commented Apr 14, 2017 at 21:42
  • Why don't you check out quick sort. en.wikipedia/wiki/Quicksort – inoabrian Commented Apr 14, 2017 at 21:55
  • I'm assuming this is a school assignment? In which case, they are most likely looking for a Bubble Sort solution – mhodges Commented Apr 14, 2017 at 22:01
  • @mhodges works fine, thanks! – Mary Commented Apr 15, 2017 at 9:16
Add a ment  | 

2 Answers 2

Reset to default 3

The approach is correct. You just had a tiny bug in the code. Instead of this:

for (var j = i - 1; j >= 0 && (array[j] < array[i]); j--) {

Do this:

for (var j = i - 1; j >= 0 && (array[j] < tmp); j--) {

This is necessary, because the value at array[i] might get overwritten with array[i-1] at the first iteration, and so in the next iteration you would be looking at the wrong value.

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'))

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745241810a292055.html

最新回复(0)