Sort By — LeetCode 2724 Python Solution

EasyJavaScript
Problem
#2724
Reading time
2 min

The problem

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.

Example

Input
arr = [5, 4, 1, 2, 3], fn = (x) => x
Output
[1, 2, 3, 4, 5]
Explanation
fn simply returns the number passed to it so the array is sorted in ascending order.

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2724. Sort By?
LeetCode 2724. Sort By is rated Easy on LeetCode.
What topics does LeetCode 2724. Sort By cover?
LeetCode 2724. Sort By is tagged JavaScript on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview