Explanation:
By default, the function sort()
javascript lexically sorts your Array. But optionally you can pass a function in the input parameter, so that it returns the desired result.
About the Sort function():
Sort([sortfunction])
Description: sorts a lexical array by default, but a function can be passed for sorting.
Parameters:
sortFunction (Function) optional:
A function that returns the desired order to be used in the sort()
.
Example:
function sortfunction(a, b){
return (a - b) //faz com que o array seja ordenado numericamente e de ordem crescente.
}
Data = [3,5,1,7,3,9,10];
Data.sort(sortfunction); //resultado: [1, 3, 3, 5, 7, 9, 10]
Only one function as a method parameter
sort()
returninga-b
would solve.– Paulo Roberto Rosa
Yeah, I already edited it! I ended up discovering it too, there in the MDN documentation.
– Miguel Angelo