Explanation:
By default, the sort()
function in javascript sorts your Array. But optionally you can pass a function on the input parameter so that it returns the desired result.
About the sort () function:
sort )
Description: sorts an array in lexical form by default, however a function can be passed to sort.
Parameters:
sortFunction (function) optional :
A function that returns the desired order to be used in 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]