zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Sort Arrays with sort #282

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

sort方法可以根据字母顺序或数字顺序对数组元素进行排序。

方法

从小到大排序

var array = [1, 12, 21, 2];
array.sort(function(a, b) {
  return a - b;
});

练习

Use sort to sort array from largest to smallest.

代码


var array = [1, 12, 21, 2];

// Only change code below this line.

array.sort(function(a,b){return b -a;});

结果显示

image

来源

https://www.freecodecamp.org/challenges/sort-arrays-with-sort