Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sorting array without sort method in javascript

let arr = [4, 32, 2, 5, 8];

for (let i = 0; i < arr.length; i++) {
  for (let j = i + 1; j < arr.length; j++) {
    if (arr[i] > arr[j]) {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
    }
  }
}
console.log("Sorted array=>", arr);
Comment

how to sort array without using sort method in javascript

function bubbleSort(array) {
  var done = false;
  while (!done) {
    done = true;
    for (var i = 1; i < array.length; i += 1) {
      if (array[i - 1] > array[i]) {
        done = false;
        var tmp = array[i - 1];
        array[i - 1] = array[i];
        array[i] = tmp;
      }
    }
  }

  return array;
}

var numbers = [12, 10, 15, 11, 14, 13, 16];
bubbleSort(numbers);
console.log(numbers);
Comment

PREVIOUS NEXT
Code Example
Javascript :: get promise result from json() javascript 
Javascript :: js read text file line by line 
Javascript :: math round 
Javascript :: json data sample 
Javascript :: smooth scroll mouse wheel javascript 
Javascript :: datatables search not working 
Javascript :: filter out object in array using two arguments 
Javascript :: convert queryset to json django 
Javascript :: uncaught typeerror is not a function javascript 
Javascript :: binary gap 
Javascript :: object get array of values 
Javascript :: why is my mongoose middleware not working 
Javascript :: javascript iterate object attribute name 
Javascript :: Uncaught TypeError: Object prototype may only be an Object or null: undefined 
Javascript :: axios error 
Javascript :: ngstyle background url angular 
Javascript :: react native textinput no keyboard 
Javascript :: disable mixed content via javascript 
Javascript :: promise.all async await 
Javascript :: data type in javascript 
Javascript :: document.getElementById(...).getContext is not a function 
Javascript :: cordova android close app with back button 
Javascript :: lodash partial match 
Javascript :: xmlhttprequest 
Javascript :: testing library react hooks 
Javascript :: javascript storage get set item 
Javascript :: js add key to object 
Javascript :: javascript get phone number from string 
Javascript :: react check if localhost 
Javascript :: read xlsx file in angular 5 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =