sort in js

Solutions on MaxInterview for sort in js by the best coders in the world

showing results for - "sort in js"
Davide
01 Nov 2018
1// Sort an array of numbers 
2let numbers = [5, 13, 1, 44, 32, 15, 500]
3
4// Lowest to highest
5let lowestToHighest = numbers.sort((a, b) => a - b);
6//Output: [1,5,13,15,32,44,500]
7
8//Highest to lowest
9let highestToLowest = numbers.sort((a, b) => b-a);
10//Output: [500,44,32,15,13,5,1]
11
Isaac
29 Oct 2019
1numArray.sort((a, b) => a - b); // For ascending sort
2numArray.sort((a, b) => b - a); // For descending sort
3
Julián
27 Oct 2016
1var points = [40, 100, 1, 5, 25, 10];
2points.sort((a,b) => a-b)
3
4
Emily
26 Feb 2017
1var items = [
2  { name: 'Edward', value: 21 },
3  { name: 'Sharpe', value: 37 },
4  { name: 'And', value: 45 },
5  { name: 'The', value: -12 },
6  { name: 'Magnetic', value: 13 },
7  { name: 'Zeros', value: 37 }
8];
9
10// sort by value
11items.sort(function (a, b) {
12  return a.value - b.value;
13});
14
15// sort by name
16items.sort(function(a, b) {
17  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
18  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
19  if (nameA < nameB) {
20    return -1;
21  }
22  if (nameA > nameB) {
23    return 1;
24  }
25
26  // names must be equal
27  return 0;
28});
Salomé
15 Apr 2018
1var names = ["Peter", "Emma", "Jack", "Mia", "Eric"];
2names.sort(); // ["Emma", "Eric", "Jack", "Mia", "Peter"]
3
4var objs = [
5  {name: "Peter", age: 35},
6  {name: "Emma", age: 21},
7  {name: "Jack", age: 53}
8];
9
10objs.sort(function(a, b) {
11  return a.age - b.age;
12}); // Sort by age (lowest first)
Liana
17 Jun 2020
1var points = [40, 100, 1, 5, 25, 10];
2points.sort((a,b) => a-b)
queries leading to this page
sort array in javascript numbersjavascript sort by functionjs order arrayarray sorrt javascritpmdn javascript array sortsort arrayhow to use sort javascriptjavasript sort a arraysort 28 29 injssortby javascriptsort array form highest to lowest javascriptsort descending javascript es6node js array sorthow to sort arrays jsjs sortsort array javascript numbersjavascript sorte arrayjs sort ascendinghow to sort array of numbers in javascriptsort array es6javascript sort methodsort array of numbers in descending order javascriptorder the number array in desc nodejsjs sort descendingsort lowest to hiest jssort values in array javascriptes6 array sortsort js arrayhow to use sort method in javascript sort method jsarrays sort methodsort with javascriptjs sort array of numbershow to sort arry in javscriptsort an array in jssort array of integers javascriptsort array javascdrtipsort syntax javascriptsort 28 29 function in javascriptjavascript sorting from small to largestsort number javascriptsort int array javascripthow to do numbers sort in javascriptsort list in javascript descendingjs sort numberjs sort 28 29how to sort numbers in an array javascriptjs sort orderjavacrtip sort arrayjavascript array sortingsort an array of integers in ascending order javascriptjs sort es6sortby in javascriptsorting in jssort javascript array of numberssort 28 29 in jsjs order functionsort javascript number arrayjs sort 28 29how to sort an array from smallest to largest javascriptjavasript array sort by valuessort ascending order javascriptsort array smallest to largest javascriptsort desc javascriptjavascript sort array of integersjavascript array order bysort numbers javascriptsort array descending javascriptsort js numbers descendingjavascript sort descendingsort in javascript array js compare strings sorthow to do a sort in javascriptsort array javascript ascending order by in javascriptjavascript integer descending order arrayjavascript sortmethod to sort array in javascripthow to sort in javascript sor 28 29 jsarr sort jssort array numbers highest to lowest javascriptjs arra sortjavascript sort array in objecthow to sort array in javascriptsort ascending and descending in javascriptorder by in jssort by ascending order javascriptsort the array in descending order in jssort an array from smallest to largest javascriptjs array sortingsort numbers from least to greatest javascriptsort an array after largest number jsjs array sort 28 29why json sort is not working in javascriptsort function jssort js functionssort an array in javascriptsort array value descending javascript stringsorting method on array javascriptsort array javascript descendingjs arr sort sort descending order jsjavascript sort array of objects ascending and descending orderhow to sort array jsorder by the array javascriptjavascript sort comparejavascript sort number arraysort an arraysort array by number value ascending order jsjavascript sort arrayarray num descending order in js sort javascriptarray sort javascripthow to sort array price low to high in jsjs sorting arraysort in descending order in javascriptsort arrays with javascripthow to sort numbers ascending sort 28 29 jssort numbers from greatest to least jsjavascript sort array descendingsort array by value jsjs number sort functionsort by number javascriptjquery array sorthow to organize your array javascriptjs sort arryhow to sort an array in descending order javascriptjs array order bysort array javascriptjavascript order array elementsdescending sort in javascriptarray prototype sort jsarray orderjavascript sort array methodsortby jssort by biggest tos mallest array javascriptjavascript sortjavascript sort functionsort by highest to lowest in javascript arraysort number jssort and array in javascriptjavascript array order by valueorder by array javascriptinbuild javascript function to sort array in descendingjs sort array string alphabeticallysort an array from smallest to biggest javascriptorder array javascript by valuejs order an arraysort by order array javascriptjavascript sort 28 29sort array largest to smallest javascriptsort array descending jssort array from biggest to smallest javascripthow to use sort 28 29 in jssort the array of objects in javascript in decending orderhow to sort number highest to lowest javascripyjavascript sort numbersjavascript array sort with functionhow to sort in jsarray sortsort array of objects javascriptjavascript sort in ascending ordermdn js sortsort array of numbers by ibggest number javascriptsort elements in an array javascriptsort array in descending order javascriptarray sort in jshow to sort a number array in javascriptsort by minimum number in javascriptsort javascript numbersarray sort jssort an array from least to greatest javascriptsort an array of numbers in javascript highest to lowestorder arrayhow to sort an array of numbers javascriptjavasrcipt sort functionsort values by highest jssort an array min max javascriptjs sorty arr sortjavascript sortingjavascript sortsort 28 mdnjavascript alphabetize an arrayarray sort desc javascriptjavascript sort datasort arrsort an array jssort a b in javascriptsort array by highest number jssort object array javascriptarrays sortsorting method in jssort the lowest ten numbers in array jshow to sort array in jsjavascript array sort ascendingjavascript array list sorthow to sort array javascriptfunction to sort jssort array from smallest to largest javascripthow to use sort function in javascriptsort numbers in arrayhow to order number array in javascripthow to sort an array in jssort on numbers javascriptsort highest to lowest js arrsorting a javascript arrayjs sort 1d arraysorting array javascriptjavascript map sortsort array numbers jshow to sort in descending order in javascriptarray sort in jssort javascript arrays sort 28function 29 in javascriptjavascript sort descending numberarray srortsort string descending javascript objectsort element javascrioptsort funtion jshow to order an array in javascriptjavscript sort byjs sort arraysort array lowest to highest javascriptjavascript array sortlist sort javascriptjavascript arrry sortjavascript sort array on valuesort array in descending javascriptjavascript sort array of numbersjscript sort arrayjavascript sort array of numbers descendingjs sort array of objectssort array javascriptjs sort numbersjavascript function for sortsort str arrays jshow to sort an array of numbers in javascriptmath sort jshow to sort array on jssort array of nums by ascending order javascriptsort number in jssort array numbers javascriptsort an array of numbers from greatest to least javascriptarray sort javascriptsorting array in descending order in javascriptarray order by value javascript sort jssort array in jsarray sorting in jshow to sort the array in javascripthow to sort an array smallest to largest jssort javascript numbersort method jsarray sort min to max jsarray sort 28javascript sort array of objectssort method javascriptsort array with numbers javascriptsort descending order array javascriptdescending ascending array value javascripthow to sort array 5d jsarray sort javascriptjs sort methodsort int array jsarray sort in javascriptjavascript sort by namejavascript sort an arraysort element in array javascriptsort array in ascending order in javascriptjs sorrt arraysort array javasckritsort of array in descending order in nodejssort descending javascriptjavascript array sort numbersjavascript sort array of numbers largest to smallestusing sort in javascriptjavascript array sort descendingorder by javascriptorder asc array jssort ascending jsorder array javascript from min to maxsort array by jssorting array of objects in javascriptsort using javascripthow to sort array lowest to highestsorting numbers array in javascript array sorting in javascriptarray sorting jsjavascript orderbyjavascript sort numbers arraytjavascript sortjavascript sort function descendingsorting in array in jsarrange array in ascending order jsarray js sortsorting jssort number array javascriptjavascript array crescent orderjavascript sort arraysort the array javascriptsort array javascriptjs array sort numbers sort array javascriptjavascript sort byarray sort function in javascripthow to sort from largest to lowerst integer javascripthow to sort an array in descending order in javascriptjs sort functionsort descending order javascriptsort numbers in descending order javascriptsort of arrayjs number sort in descending ordersort object in jssort 28 29 jssort int array in javascriptsort by javascriptjs order array byfunction in sort method jsascending order an array matlab javascript w3schoolsjavascript sort array lowest to highestsort array in desc order javascriptjavascript array sortorder number array javascriptarray js sorthow to sort an array in javascriptjavascript sort array of stringssort the array numerically descending javascriptsort obj of integers jsjs aray sortsorting array in javascriptjavascript sort examplesort array value descending javascriptjavascript sort highest to lowesthow to sort an array javascriptsort jssort array elements in javascriptsort array number sort in javascriptjs sort arrathow to sort descending in javascriptjavascript sort array alphabetically sort javascriptorderby sort nodejsjs sort array with functionsort arraysjavascript sort array numbers ascendinglowest to highest number sorter javascriptarray sort in javascriptsort function of array in javascript sort functionsorting array from lowest to highestarray sort byorder array javascriptsort array of object jshow to sort an array by highest number in javascriptsortarray javascriptsort array highest to lowest javascriptorder array from least to greatest javascriptdescending order in jssort method in javascripthow to sort in javascritp arrayjs how to sort an array of numberssort 28 29order javascriptarray sortingfunction to sort array in javascriptorder array jssort array javascript 5corder an arrayhow sort array jssort javascript arrayjavascript sorting arrayhow to put array elements in order from lowest to highest in javascriptsort array of object es6js use sort on arraysorting arrayjavascript sort array highest to lowest sort from highest to lowest javascriptjavascript sort array by descendingorder jsjavascript sort array by highest valuehow to sort numbers in javascriptsort array of numbers javascriptsort method of array in javascriptsort from low to high javascriptsort array from lowest to highest javascriptsort array by highest value javascript sort 28 29 in javascriptalphabetize site 3ahttps 3a 2f 2fdeveloper mozilla org 2fen us 2fdocs 2fweb 2fjavascript 2freference 2fglobal objects 2farrayjs sort from max to minjs sort bynode js sort arraysort number in javascripthjs array sort methodsarr sortorder array by value javascriptjavascript order arrayhow to sort array in descending order in javascriptsort array of numbers ascendingwht is sort 28 29 javascriptjavascript sort least to greatestsort an array of numbers javascriptjs array sortsort elements from least to greatest jsjs orderbysort in descending order javascriptsort array of jsjs sortsort array in javascriptjavascript sort by highest numberorderby jssorting array jsjavasscript sort arraysort array from smallest to largest jshow to sort array from highest to lowest jquery json sort alphabeticallyorder array in javascriptsort low to high javascriptsorting in javascriptsort elements in arraysort roll numbers from json in javascripthow to sort arraysort from highest to lowest javascriptsort array jsjavascript array orderhow to sort an integer array in javascriptsort array by value javascriptjavascript order array by value ascendingsort js arrayarray sortsort lowest to highest jssort function in javascriptorder array min to max javascriptjavascript array sort by highest valuessort array javasscriporder list jshow to sort in descending order javascriptjavascript number sortmeaning of sort 28 29 array method in javascriptssort an array by nodejsarray sort 28 29 jsarray soryarray sort ascending javascriptarray js sortsort array mdnjavascript arrange array in ascending orderorder by jssort int array least to greatest javascriptsort example jssort 28 29 jssort an array javascriptjavascript ascending order sort functionjs sortbyjava script sortjavascript sort array but numbersort array in order javascriptorderby method jssort js descorder by name jssort javascriptjs sort in orderarray sort jshow to sort array by order jsjavascript program to sort the items of an arrayarray sort descendingjs sortingjquery reorder arraysort array in descending order jshow to sort an array in ascending order in javascriptsort array shortest to largest string javascript mdn array sortsort 28 29 in javascript sort method javascriptsort by field ascending text jsjava script sort arrayjs arrange arraysort by array in javascriptsort in jsnumber sort javascriptsort a js arrysort values in object javascript using sort methodarray sortsort function javascript sort in javascriptjavascript sort number listorder numbers in array javascriptsort array in ascending order javascripthow to order array in javascripthow to sort numbers from highest to lowest jsjs sort array descendingjs sort elementssort largest to smallest javascriptjs array sortsort array jssort 28 29 javascripthow to sort numbers in array in javascriptarray sort javascriptjs sortarrayhow to javascript sortsort function in jssort func jshow to sort items in a list by number jsjavascript array sort number dessort string list javascriptsort in js