Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

function(a, b){return b - a}

The sort() method needs to know the relation between each two elements in order to sort them. 
For each pair (according to the algorithm used) it calls the function then based on the return value,
may swap them. 
Note that a-b returns a negative value if b>a and a positive one if a>b. 
That leads to an ascending order.

Just for test, replace a-b with b-a and you will get a descending order. 
You may even make it more complicated,
 sort them based on (for example) their least significant digit:

a.sort(function() {return (a%10) - (b%10);}
Comment

PREVIOUS NEXT
Code Example
Javascript :: json to list flutter 
Javascript :: load a config file discordjs 
Javascript :: how to change image source using javascript 
Javascript :: get value of input jqueyr 
Javascript :: regex is empty string javascript 
Javascript :: input field take only number and one comma 
Javascript :: ionic cordova icon notification 
Javascript :: js two array combining with id 
Javascript :: discord.js on ready 
Javascript :: use onchange with react select 
Javascript :: prependchild in javascript 
Javascript :: link button material ui 
Javascript :: remove duplicates from array 
Javascript :: date js 
Javascript :: axios x-api-key for all 
Javascript :: how to disable onclick event in javascript 
Javascript :: js string contains 
Javascript :: “javascript sleep 1 second” 
Javascript :: regex for non empty string 
Javascript :: owl.js cdn 
Javascript :: express redirect 
Javascript :: sls invoke local 
Javascript :: mongoose connect to mongodb 
Javascript :: Deep copy objects js JSON methods 
Javascript :: vuex use state in action 
Javascript :: jquery check checkbox 
Javascript :: add event listener on width screen resize 
Javascript :: jquery table row count 
Javascript :: using .includes for an array of objects js 
Javascript :: how to remove all child elements in javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =