1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/sort-by.json

63 lines
5.7 KiB
JSON

{
"data": {
"question": {
"questionId": "2860",
"questionFrontendId": "2724",
"boundTopicId": null,
"title": "Sort By",
"titleSlug": "sort-by",
"content": "<p>Given an array <code>arr</code> and a function <code>fn</code>, return a sorted array <code>sortedArr</code>. You can assume&nbsp;<code>fn</code>&nbsp;only returns numbers and those numbers determine the sort order of&nbsp;<code>sortedArr</code>. <code>sortedArray</code> must be sorted in <strong>ascending order</strong> by <code>fn</code> output.</p>\n\n<p>You may assume that <code>fn</code> will never duplicate numbers for a given array.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [5, 4, 1, 2, 3], fn = (x) =&gt; x\n<strong>Output:</strong> [1, 2, 3, 4, 5]\n<strong>Explanation:</strong> fn simply returns the number passed to it so the array is sorted in ascending order.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [{&quot;x&quot;: 1}, {&quot;x&quot;: 0}, {&quot;x&quot;: -1}], fn = (d) =&gt; d.x\n<strong>Output:</strong> [{&quot;x&quot;: -1}, {&quot;x&quot;: 0}, {&quot;x&quot;: 1}]\n<strong>Explanation:</strong> fn returns the value for the &quot;x&quot; key. So the array is sorted based on that value.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [[3, 4], [5, 2], [10, 1]], fn = (x) =&gt; x[1]\n<strong>Output:</strong> [[10, 1], [5, 2], [3, 4]]\n<strong>Explanation:</strong> arr is sorted in ascending order by number at index=1.&nbsp;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>arr is a valid JSON array</code></li>\n\t<li><code>fn is a function that returns a number</code></li>\n\t<li><code>1 &lt;=&nbsp;arr.length &lt;= 5 * 10<sup>5</sup></code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 125,
"dislikes": 25,
"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": "type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };\ntype Fn = (value: JSONValue) => number\n\nfunction sortBy(arr: JSONValue[], fn: Fn): JSONValue[] {\n\t\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"17K\", \"totalSubmission\": \"20.8K\", \"totalAcceptedRaw\": 16954, \"totalSubmissionRaw\": 20824, \"acRate\": \"81.4%\"}",
"hints": [],
"solution": {
"id": "1989",
"canSeeDetail": true,
"paidOnly": false,
"hasVideoSolution": false,
"paidOnlyVideo": true,
"__typename": "ArticleNode"
},
"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\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}