JavaScript数组的sort方法常规用法
2021-02-07 11:19
标签:tps https java 字符串数组 function png sort csdn lan JavaScript数组的sort方法常规用法 标签:tps https java 字符串数组 function png sort csdn lan 原文地址:https://www.cnblogs.com/TomHe789/p/12775896.html // 对普通数组进行排序
let nums = [1, 3, 4, -4, 2, 5, 1];
nums.sort(function (a, b) {
// 升序
// return a - b;
//降序
return b - a;
});
console.log(nums);
// 对字符串数组进行排序
let arr = ["asd", "as", "a", "asdf"];
arr.sort(function (str1, str2) {
// 升序
// return str1.length - str2.length;
//降序
return str2.length - str1.length;
});
console.log(arr);
// 对对象数组进行排序
let students = [
{name: "zs", age: 12},
{name: "ls", age: 7},
{name: "ww", age: 30},
{name: "zl", age: 10},
];
students.sort(function (stu1, stu2) {
// return stu1.age - stu2.age;
return stu2.age - stu1.age;
});
console.log(students);
文章标题:JavaScript数组的sort方法常规用法
文章链接:http://soscw.com/index.php/essay/52166.html