{ "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.

\n\n

You may assume that fn will never duplicate numbers for a given array.

\n\n

 

\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\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 17, "dislikes": 3, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "[5,4,1,2,3]\n(x) => x\n[{\"x\":1},{\"x\": 0},{\"x\": -1}]\n(x) => x.x\n[[3,4],[5,2],[10,1]]\n(x) => x[1]", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */\nvar sortBy = function(arr, fn) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function sortBy(arr: any[], fn: Function): any[] {\n\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"868\", \"totalSubmission\": \"1K\", \"totalAcceptedRaw\": 868, \"totalSubmissionRaw\": 1043, \"acRate\": \"83.2%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[5,4,1,2,3]\n(x) => x", "metaData": "{\n \"name\": \"sortBy\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For 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.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }