了解 JavaScript 中的数组排序

152 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第24天,点击查看活动详情 原文链接:Obodo David hello,各位小伙伴,你们真的了解JavaScript中的数组排序吗?本文目的是教你 JavaScript 中的数组排序。

1.排序数组 - 字母

  • 要按升序排序:

我们使用 .sort() 方法在 Array 中按升序对字母进行排序。 .sort()方法返回排序后的数组,同时原数组也会被改变。 语法:

arr.sort()

例子:

const cars = ["BMW", "BENZ", "BUGATI", "MARUTHI","INNOVA"];
console.log(cars.sort());

输出:

[ 'BENZ', 'BMW', 'BUGATI', 'INNOVA', 'MARUTHI' ]
  • 降序排序:

我们使用 .reverse() 方法在 Array 中按降序对字母进行排序。 .reverse() 返回逆反之后的数据。 语法:

arr.sort();
arr.reverse();

例子:

const cars = ["BMW", "BENZ", "BUGATI", "MARUTHI","INNOVA"];
cars.sort();
console.log(cars.reverse());

输出:

[ 'MARUTHI', 'INNOVA', 'BUGATI', 'BMW', 'BENZ' ]

2.排序数字 - 数字

  • 按从低到高排序:

.sort()方法可以接受一个函数,这个函数用来比较两个数之间的大小 我们使用 .sort((a,b)=>a-b) 方法对数组中的数字进行升序排序。

arr.sort((a,b)=>a-b);

例子:

const num = ["50", "88", "1", "600","30"];
console.log(num.sort((a,b)=>a-b));

输出:

[ '1', '30', '50', '88', '600' ]
  • 要按从高到低的顺序排序:

我们使用 .sort((a,b)=>b-a) 方法对数组中的数字进行降序排序。

句法:

arr.sort((a,b)=>b-a);

例子:

const num = ["50", "88", "1", "600","30"];
console.log(num.sort((a,b)=>b-a));

输出:

[ '600', '88', '50', '30', '1' ]

3.随机排序

我们使用 .sort((a,b)=>0.5-Math.random()) 方法对数组中的数字进行随机排序。

语法:

arr.sort((a,b)=>0.5-Math.random());

例子:

const num = ["50", "88", "1", "600","30"];
console.log(num.sort((a,b)=>0.5-Math.random()));

输出:

[ '600', '50', '30', '88', '1' ]

4.在对象数组中排序

我们使用 .sort((a,b)=>a.property-b.property) 方法对数组中的数字进行升序排序。

我们使用 .sort((a,b)=>b.property-a.property) 方法对数组中的数字进行降序排序。

语法:

arr.sort((a,b)=>a.property-b.property);

例子:

const cars = [  {type:"Volvo", year:2016},  {type:"Saab", year:2001},  {type:"BMW", year:2010}];
console.log(cars.sort((a, b)=>a.year - b.year));

输出:

[
  { type: 'Saab', year: 2001 },
  { type: 'BMW', year: 2010 },
  { type: 'Volvo', year: 2016 }
]

5. 用 Math.min( ) 和 Math.max( ) 排序

  • 使用 Math.max 查找最大值:

我们使用 Math.max.apply(null, arr) 方法来查找数组中的最高数。

语法:

Math.max.apply(null, arr);

例子:

function myArrayMax(arr) {
  return Math.max.apply(null, arr);
}
console.log(myArrayMax([12,56,700,20,90]));

输出:

700
  • 使用 Math.min 找到最小值:

我们使用 Math.min.apply(null, arr) 方法来查找数组中的最小数。

语法:

Math.min.apply(null, arr);

例子:

function myArrayMax(arr) {
  return Math.min.apply(null, arr);
}
console.log(myArrayMax([12,56,700,20,90]));

输出:

12

6.程序以升序,降序显示数组。最高值和数组的最小值

const arr=[ 600, 50, 30, 88, 1 ];
console.log("Ascending Order:",arr.sort((a,b)=>a-b));
console.log("Descending Order:",arr.sort((a,b)=>b-a));
console.log("Highest Value:",Math.max.apply(null,arr));
console.log("Lowest Value:",Math.min.apply(null,arr));

输出:

Ascending Order: [ 1, 30, 50, 88, 600 ]
Descending Order: [ 600, 88, 50, 30, 1 ]
Highest Value: 600
Lowest Value: 1

结论:

  • 我们使用 .sort() 方法在 Array 中按升序对字母进行排序。
  • 我们使用 .reverse() 方法在 Array 中按降序对字母进行排序。
  • 我们使用 .sort((a,b)=>0.5-Math.random()) 方法对数组中的数字进行随机排序。
  • 我们使用 .sort((a,b)=>a.property-b.property) 方法对数组中的数字进行升序排序。
  • 我们使用 .sort((a,b)=>b.property-a.property) 方法对数组中的数字进行降序排序。
  • 我们使用 Math.max.apply(null, arr) 方法来查找数组中的最高数。
  • 我们使用 Math.min.apply(null, arr) 方法来查找数组中的最小数。