2023-06-12 23:05:37 +08:00
{
"data" : {
"question" : {
"questionId" : "2860" ,
"questionFrontendId" : "2724" ,
"categoryTitle" : "JavaScript" ,
"boundTopicId" : 2302688 ,
"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 <code>fn</code> only returns numbers and those numbers determine the sort order of <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> </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) => 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 = [{"x": 1}, {"x": 0}, {"x": -1}], fn = (d) => d.x\n<strong>Output:</strong> [{"x": -1}, {"x": 0}, {"x": 1}]\n<strong>Explanation:</strong> fn returns the value for the "x" 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) => 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. \n</pre>\n\n<p> </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 <= arr.length <= 5 * 10<sup>5</sup></code></li>\n</ul>\n" ,
"translatedTitle" : "排序方式" ,
"translatedContent" : "<p>给定一个数组 <code>arr</code> 和一个函数 <code>fn</code>,返回一个排序后的数组 <code>sortedArr</code>。你可以假设 <code>fn</code> 只返回数字,并且这些数字决定了 <code>sortedArr</code> 的排序顺序。<code>sortedArr</code> 必须按照 <code>fn</code> 的输出值 <strong>升序</strong> 排序。</p>\n\n<p>你可以假设对于给定的数组,<code>fn</code> 不会返回重复的数字。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1: </strong></p>\n\n<pre>\n<b>输入:</b>arr = [5, 4, 1, 2, 3], fn = (x) => x\n<b>输出:</b>[1, 2, 3, 4, 5]\n<b>解释:</b>fn 只是返回传入的数字,因此数组按升序排序。\n</pre>\n\n<p><strong class=\"example\">示例 2: </strong></p>\n\n<pre>\n<b>输入:</b>arr = [{\"x\": 1}, {\"x\": 0}, {\"x\": -1}], fn = (d) => d.x\n<b>输出:</b>[{\"x\": -1}, {\"x\": 0}, {\"x\": 1}]\n<b>解释:</b>fn 返回 \"x\" 键的值,因此数组根据该值排序。\n</pre>\n\n<p><strong class=\"example\">示例 3: </strong></p>\n\n<pre>\n<b>输入:</b>arr = [[3, 4], [5, 2], [10, 1]], fn = (x) => x[1]\n<b>输出:</b>[[10, 1], [5, 2], [3, 4]]\n<b>解释:</b>数组按照索引为 1 处的数字升序排序。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>arr 是一个有效的 JSON 数组</code></li>\n\t<li><code>fn 是一个函数,返回一个数字</code></li>\n\t<li><code>1 <= arr.length <= 5 * 10<sup>5</sup></code></li>\n</ul>\n" ,
"isPaidOnly" : false ,
"difficulty" : "Easy" ,
2023-12-09 18:42:21 +08:00
"likes" : 4 ,
2023-06-12 23:05:37 +08:00
"dislikes" : 0 ,
"isLiked" : null ,
"similarQuestions" : "[]" ,
"contributors" : [ ] ,
2023-12-09 18:42:21 +08:00
"langToValidPlayground" : "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}" ,
2023-06-12 23:05:37 +08:00
"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" ,
2023-12-09 18:42:21 +08:00
"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};" ,
2023-06-12 23:05:37 +08:00
"__typename" : "CodeSnippetNode"
}
] ,
2023-12-09 19:57:46 +08:00
"stats" : "{\"totalAccepted\": \"2.9K\", \"totalSubmission\": \"3.8K\", \"totalAcceptedRaw\": 2923, \"totalSubmissionRaw\": 3780, \"acRate\": \"77.3%\"}" ,
2023-06-12 23:05:37 +08:00
"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 ,
2023-12-09 18:42:21 +08:00
"envInfo" : "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}" ,
2023-06-12 23:05:37 +08:00
"book" : null ,
"isSubscribed" : false ,
"isDailyQuestion" : false ,
"dailyRecordStatus" : null ,
"editorType" : "CKEDITOR" ,
"ugcQuestionId" : null ,
"style" : "LEETCODE" ,
"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]" ,
"__typename" : "QuestionNode"
}
}
}