Sort By - Problem
Sort By Custom Function
You are given an array
For example, if you have an array of objects representing students, you might want to sort them by their grades (using a function that extracts the grade) rather than alphabetically by name.
Goal: Return a new sorted array where elements are ordered by the ascending values returned by the function
Key Points:
• The function
• Sort in ascending order by function output
• The function will never return duplicate numbers for the given array
• You must preserve the original elements, just reorder them
You are given an array
arr and a function fn that transforms each element into a number. Your task is to sort the array based on the output of the function, not the original values.For example, if you have an array of objects representing students, you might want to sort them by their grades (using a function that extracts the grade) rather than alphabetically by name.
Goal: Return a new sorted array where elements are ordered by the ascending values returned by the function
fn.Key Points:
• The function
fn always returns numbers• Sort in ascending order by function output
• The function will never return duplicate numbers for the given array
• You must preserve the original elements, just reorder them
Input & Output
example_1.js — Basic Number Sorting
$
Input:
arr = [5, 4, 1, 3, 2], fn = (x) => x
›
Output:
[1, 2, 3, 4, 5]
💡 Note:
The function returns the number itself, so we sort in ascending numerical order. Each element is compared by its own value.
example_2.js — Reverse Order
$
Input:
arr = [{'x': 1}, {'x': 0}, {'x': -1}], fn = (d) => -d.x
›
Output:
[{'x': 1}, {'x': 0}, {'x': -1}]
💡 Note:
The function returns the negative of x. So fn({'x': 1}) = -1, fn({'x': 0}) = 0, fn({'x': -1}) = 1. Sorting by these values gives us the reverse order of x values.
example_3.js — String Length Sorting
$
Input:
arr = ['hello', 'world', 'hi', 'a'], fn = (s) => s.length
›
Output:
['a', 'hi', 'hello', 'world']
💡 Note:
Sorting by string length: 'a' (length 1), 'hi' (length 2), 'hello' (length 5), 'world' (length 5). Since hello comes before world in original array and they have same length, order is preserved.
Constraints
- 1 ≤ arr.length ≤ 1000
- fn returns distinct integers for each element
- arr can contain any valid JSON values
- fn will always return a number for comparison
Visualization
Tap to expand
Understanding the Visualization
1
Extract Sort Keys
Apply the function to each element to get comparable values
2
Compare Elements
Use these values to determine the correct order
3
Rearrange Array
Move elements to their final sorted positions
4
Return Result
Output the array with original elements in new order
Key Takeaway
🎯 Key Insight: The custom function transforms each element into a comparable value, allowing us to sort by any criteria while preserving the original elements in their new order.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code