The JavaScript sort()
function may or may not take a parameter.
The parameter would be a function - meaning what function is to be used to assess which of two elements should be before the other
.
The n array is made of strings representing numbers.
Doing a simple sort()
without a function, an alphabetical order would be used: the result would be
"1", "10", "25"... "5"
并且不正确。
提供一种功能,sortNumber
, 计票sort
,每次<代码>sort<>/code>想要知道这两个项目中哪些是摆在另一个项目面前时,即以两个阵列要素标明该功能。
因此,sortNumber
提供了两个项目,即:<代码>数字操作,以换取
- a negative value, meaning a is before b
- a positive value, b is before a
- zero: they re equal in terms of order (order doesn t matter).