Discuss / JavaScript / 练习题,(升序)排序简化

练习题,(升序)排序简化

Topic source

-Otokaze-

#1 Created at ... [Delete] [Delete and Lock User]
'use strict';

var arr = [10, 20, 1, 2];

// 升序
arr.sort((x, y) => x === y ? 0 : x < y ? -1 : 1);
console.log(arr); // [1, 2, 10, 20]

// 降序
arr.sort((x, y) => x === y ? 0 : x < y ? 1 : -1);
console.log(arr); // [20, 10, 2, 1]

曦瑞之端

#2 Created at ... [Delete] [Delete and Lock User]

arr.sort((x, y) => { return (x-y) });


  • 1

Reply