Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Sort numbers from an array in javascript

const bignumbers = [66, 58, 81, 444, 92, 9, 6, 13, 2];
const sortedNumbers = bignumbers.sort(function (a, b) {
    return a - b;
})
console.log(sortedNumbers);
//Output: [2, 6, 9, 13, 58,66, 81, 92, 444]
Comment

sort in javascript array with numbers

//sort is function in js which sort according to alphanumerical
//for array with number it is little twist
const items= ["Banana","Orange","Apple"];
const ratings = [92,52,2,22]
console.log(items.sort())// reuturn ["Apple","Banana","Orange]
//for array with number
ratings.sort(function(a,b){
return a-b; //ascending for decending b-a
// return is negative a is sorted befor b 
// positive b is sorted before a
// if they are the same is 0 then nothing changes.
})
Comment

js sort number array

objs.sort((a,b) => a.last_nom - b.last_nom); // b - a for reverse sort
Comment

js sort number array

// ascending (normal)
numArray.sort((a,b) => a-b);

// decending (reverse)
numArray.sort((a,b) => b-a);
Comment

sort numbers in array in js

const numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

// [1, 2, 3, 4, 5]
Comment

sort numbers in array javascript

function sortNumber(a, b) {
  return a - b;
}
Arr.sort(sortNumber);
Comment

sort array of numbers js

var numArray = [140000, 104, 99];
numArray.sort(function(a, b) {
  return a - b;
});

console.log(numArray);
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript comments 
Javascript :: split string with last character and return array in javascript 
Javascript :: js or 
Javascript :: getelementsbytagname 
Javascript :: running a function in a function javascript 
Javascript :: reactstrap dropdown 
Javascript :: jquery if else 
Javascript :: append raw html javascript 
Javascript :: for in js 
Javascript :: jspdf reduce size file 
Javascript :: setProps jest 
Javascript :: js reverse number 
Javascript :: to higher case js 
Javascript :: datatables modify rows 
Javascript :: Uncaught TypeError: document.getContext is not a function 
Javascript :: or operator js 
Javascript :: delete from array javascript 
Javascript :: reduce to calculate sum react 
Javascript :: stack in javascript 
Javascript :: how to use labels in javascript 
Javascript :: nestjs custom error class validator 
Javascript :: how to get the text of a clicked elemet by javascript 
Javascript :: mongoose get value 
Javascript :: Vuejs + Laravel router redirection issue 
Javascript :: redux saga fetch api 
Javascript :: how to set css in hbs in express 
Javascript :: xmlhttprequest object 
Javascript :: js base64 encode 
Javascript :: function prototype in javascript 
Javascript :: vue component lifecycle 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =