Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to sort array numbers in ascending order in javascript

// how to sort array numbers in javascript without mutating original array.
const numbers = [100, 25, 1, 5];
const sorted = numbers.slice().sort((a, b) =>  a - b); // returns a new sorted array

console.log(numbers); // [100, 25, 1, 5]
console.log(sorted); // [1, 5, 25, 100]
Comment

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 string mixed with numbers javascript

const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true })
console.log(['A1', 'A10', 'A11', 'A12', 'A2', 'A3', 'A4', 'B10', 'B2', 'F1', 'F12', 'F3'].sort(sortAlphaNum))
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

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 string mixed with numbers javascript

var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;

function sortAlphaNum(a, b) {
  var aA = a.replace(reA, "");
  var bA = b.replace(reA, "");
  if (aA === bA) {
    var aN = parseInt(a.replace(reN, ""), 10);
    var bN = parseInt(b.replace(reN, ""), 10);
    return aN === bN ? 0 : aN > bN ? 1 : -1;
  } else {
    return aA > bA ? 1 : -1;
  }
}
console.log(
["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum)
)
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to add eventlister to multiple variable 
Javascript :: how to create an html element in javascript without document 
Javascript :: using plice to remove an element from an array in react 
Javascript :: puppeteer set up code 
Javascript :: string to svg react 
Javascript :: passing props in react functional components 
Javascript :: smtp testing 
Javascript :: how to give args type in nestjs graphql for array of input 
Javascript :: react select options 
Javascript :: if and else shorthand 
Javascript :: could not decode base64 cloudinary 
Javascript :: importing sha256 hashing algorithm 
Javascript :: save text of div to localStorage, update localStorage when text is changed 
Javascript :: cheapest node js hosting 
Javascript :: java script or js circle collision 
Javascript :: mobile nav react npm 
Javascript :: check if field exists in object javascript 
Javascript :: get unique id angular 
Javascript :: Discord.client on 
Javascript :: jquery send to another page 
Javascript :: how to use fetch api 
Javascript :: javascript save as pdf 
Javascript :: paper in material ui 
Javascript :: JavaScript Extract Values 
Javascript :: local forage 
Javascript :: Use jsx extension react-native 
Javascript :: update nested formgroup angular 
Javascript :: list of dictionaries javascript 
Javascript :: react native bottom sheet above the bottom menu 
Javascript :: gps nodejs 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =