Answers for "Write a function that takes an array of numbers, sorts the elements in ascending order and returns the sorted array? Note: Don't use the sort() method."

13

javascript orderby

var items = [
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic', value: 13 },
  { name: 'Zeros', value: 37 }
];

// sort by value
items.sort(function (a, b) {
  return a.value - b.value;
});

// sort by name
items.sort(function(a, b) {
  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
});
Posted by: Guest on May-30-2020
2

js sort asendenet

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
Posted by: Guest on October-08-2020
0

how to sort array least to greatest javascript stACK

function sortArray(array) {
  var temp = 0;
  for (var i = 0; i < array.length; i++) {
    for (var j = i; j < array.length; j++) {
      if (array[j] < array[i]) {
        temp = array[j];
        array[j] = array[i];
        array[i] = temp;
      }
    }
  }
  return array;
}

console.log(sortArray([3,1,2]));
Posted by: Guest on November-26-2019

Code answers related to "Write a function that takes an array of numbers, sorts the elements in ascending order and returns the sorted array? Note: Don't use the sort() method."

Browse Popular Code Answers by Language