如何在 JavaScript 中正确对整数数组进行排序?

javascriptweb developmentfront end technologyobject oriented programming更新于 2026/2/10 13:00:17

要对整数数组进行排序,请在 JavaScript 中使用 sort()。

示例

以下是代码 −

var arrayOfIntegers = [67, 45, 98, 52];
arrayOfIntegers.sort(function (first, second) {
   return first - second;
});
console.log(arrayOfIntegers);

要运行上述程序,您需要使用以下命令 −

node fileName.js.

这里,文件名是 demo310.js。

输出

这将产生以下输出 −

PS C:\Users\Amit\javascript-code> node demo310.js
[ 45, 52, 67, 98 ]

相关文章


有用资源