Skip to content

Commit 52c7bbd

Browse files
committed
Add solution #2724
1 parent ee54aee commit 52c7bbd

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@
408408
2695|[Array Wrapper](./2695-array-wrapper.js)|Easy|
409409
2703|[Return Length of Arguments Passed](./2703-return-length-of-arguments-passed.js)|Easy|
410410
2721|[Execute Asynchronous Functions in Parallel](./2721-execute-asynchronous-functions-in-parallel.js)|Medium|
411+
2724|[Sort By](./2724-sort-by.js)|Easy|
411412
3042|[Count Prefix and Suffix Pairs I](./3042-count-prefix-and-suffix-pairs-i.js)|Easy|
412413
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
413414
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|

solutions/2724-sort-by.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 2724. Sort By
3+
* https://leetcode.com/problems/sort-by/
4+
* Difficulty: Easy
5+
*
6+
* Given an array arr and a function fn, return a sorted array sortedArr.
7+
* You can assume fn only returns numbers and those numbers determine the
8+
* sort order of sortedArr. sortedArr must be sorted in ascending order
9+
* by fn output.
10+
*
11+
* You may assume that fn will never duplicate numbers for a given array.
12+
*/
13+
14+
/**
15+
* @param {Array} arr
16+
* @param {Function} fn
17+
* @return {Array}
18+
*/
19+
var sortBy = function(arr, fn) {
20+
return arr.sort((a, b) => fn(a) - fn(b));
21+
};

0 commit comments

Comments
 (0)