-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2724-SortBy.js
More file actions
38 lines (32 loc) · 1.44 KB
/
2724-SortBy.js
File metadata and controls
38 lines (32 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 2724. Sort By
// Given an array arr and a function fn, return a sorted array sortedArr.
// You can assume fn only returns numbers and those numbers determine the sort order of sortedArr.
// sortedArray must be sorted in ascending order by fn output.
// You may assume that fn will never duplicate numbers for a given array.
// Example 1:
// Input: arr = [5, 4, 1, 2, 3], fn = (x) => x
// Output: [1, 2, 3, 4, 5]
// Explanation: fn simply returns the number passed to it so the array is sorted in ascending order.
// Example 2:
// Input: arr = [{"x": 1}, {"x": 0}, {"x": -1}], fn = (d) => d.x
// Output: [{"x": -1}, {"x": 0}, {"x": 1}]
// Explanation: fn returns the value for the "x" key. So the array is sorted based on that value.
// Example 3:
// Input: arr = [[3, 4], [5, 2], [10, 1]], fn = (x) => x[1]
// Output: [[10, 1], [5, 2], [3, 4]]
// Explanation: arr is sorted in ascending order by number at index=1.
// Constraints:
// arr is a valid JSON array
// fn is a function that returns a number
// 1 <= arr.length <= 5 * 10^5
/**
* @param {Array} arr
* @param {Function} fn
* @return {Array}
*/
var sortBy = function(arr, fn) {
return arr.sort((a,b) => fn(a) - fn(b));
};
console.log(sortBy([5, 4, 1, 2, 3],(x) => x)) // [1, 2, 3, 4, 5]
console.log(sortBy([{"x": 1}, {"x": 0}, {"x": -1}],(d) => d.x)) // [{"x": -1}, {"x": 0}, {"x": 1}]
console.log(sortBy([[3, 4], [5, 2], [10, 1]],(x) => x[1])) // [[10, 1], [5, 2], [3, 4]]