Ejemplo 1: clasificación array de mayor a menor javascript
// Sort an array of numbers let numbers =[5,13,1,44,32,15,500]// Lowest to highestlet lowestToHighest = numbers.sort((a, b)=> a - b);//Output: [1,5,13,15,32,44,500]//Highest to lowestlet highestToLowest = numbers.sort((a, b)=> b-a);//Output: [500,44,32,15,13,5,1]
Ejemplo 2: clasificación del elemento numérico de la matriz en javascript
let numbers =[0,1,2,3,10,20,30];
numbers.sort((a, b)=> a - b);console.log(numbers);
Ejemplo 3: ordenación de JavaScript
var names =["Peter","Emma","Jack","Mia","Eric"];
names.sort();// ["Emma", "Eric", "Jack", "Mia", "Peter"]var objs =[name:"Peter", age:35,name:"Emma", age:21,name:"Jack", age:53];
objs.sort(function(a, b)return a.age- b.age;);// Sort by age (lowest first)
Ejemplo 4: JavaScript ascendente y descendente
// ascending and discending for numberconst arr1 =[21,2100,2,35000];const arr2 =[21,2100,2,35000];let ascN = arr1.sort((f, s)=> f - s);let dscN = arr2.sort((f, s)=> s - f);// ascending and discending for stringconst arr3 =['21','2100','2','35000'];const arr4 =['21','2100','2','35000'];let ascS = arr3.sort((f, s)=> f.length- s.length);let dscS = arr4.sort((f, s)=> s.length- f.length);
Ejemplo 5: string ordenar javascript
var names =["Peter","Emma","Jack","Mia"];var sorted = names.sort();// ["Emma", "Jack", "Mia", "Peter"]
Tienes la posibilidad dar recomendación a esta reseña si lograste el éxito.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)