{ "data": { "question": { "questionId": "2860", "questionFrontendId": "2724", "boundTopicId": null, "title": "Sort By", "titleSlug": "sort-by", "content": "
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.
\n
Example 1:
\n\n\nInput: arr = [5, 4, 1, 2, 3], fn = (x) => x\nOutput: [1, 2, 3, 4, 5]\nExplanation: fn simply returns the number passed to it so the array is sorted in ascending order.\n\n\n
Example 2:
\n\n\nInput: arr = [{"x": 1}, {"x": 0}, {"x": -1}], fn = (d) => d.x\nOutput: [{"x": -1}, {"x": 0}, {"x": 1}]\nExplanation: fn returns the value for the "x" key. So the array is sorted based on that value.\n\n\n
Example 3:
\n\n\nInput: arr = [[3, 4], [5, 2], [10, 1]], fn = (x) => x[1]\nOutput: [[10, 1], [5, 2], [3, 4]]\nExplanation: arr is sorted in ascending order by number at index=1. \n\n\n
\n
Constraints:
\n\narr is a valid JSON array
fn is a function that returns a number
1 <= arr.length <= 5 * 105
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"typescript\": [\"Typescript\", \"TypeScript 4.5.4, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }