0
Q:

sort function explained javascript

const unsorted = ['d', 'd', 'h', 'r', 'v', 'z', 'f', 'c', 'g'];
const sorted = unsorted.sort();

console.log(sorted);
//["c", "d", "d", "f", "g", "h", "r", "v", "z"]


const unsortedNums = [45, 56, 3, 3, 4, 6, 7, 45, 1];
const sortedNums = unsortedNums.sort((a, b) => {
	return a - b;
});

console.log(sortedNums);
//[1, 3, 3, 4, 6, 7, 45, 45, 56]
3
var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
7
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

// [1, 2, 3, 4, 5]
3
//sorts arrays of numbers
function myFunction() {
  points.sort(function(a, b){return a-b});
  document.getElementById("demo").innerHTML = points;
}
2
var sortedArray = myArray.sort(function(a,b){
                                   if (a.name < b.name)
                                      return -1;
                                   else if (a.name == b.name)
                                      return 0;
                                   else
                                      return 1;
                               });
0

New to Communities?

Join the community