2724. Sort By Solution
IntuitionThis problem is focused on custom sorting using higher-order functions.
We are required to implement a utility function sortBy(arr, fn) that:
Sorts an
# **Intuition**

This problem is focused on **custom sorting using higher-order functions**.  
We are required to implement a utility function `sortBy(arr, fn)` that:

- Sorts an array **based on a transformation** applied to each element using a callback function `fn`.
- The goal is to allow **dynamic and flexible sorting** criteria, depending on the logic inside `fn`.

Understanding this helps improve skills in **custom comparator functions**, which is a vital part of advanced array manipulations in JavaScript.

---

# **Understanding `sort()` in JavaScript**

✅ **What is `Array.prototype.sort()`?**  
The `sort()` method sorts the elements of an array **in place** and returns the sorted array. It takes an **optional compare function** to determine the sort order.

✅ **Why use a custom comparator?**  
Custom comparator functions are essential when:

- Sorting objects or values **based on derived criteria**
- You need **ascending/descending** control
- You want to **sort dynamically** based on external logic

---

# **Approach**

We explored **two main approaches** for implementing the `sortBy` function.

---

# 🔹 **Approach 1: Simple Arithmetic Comparator**

```js
var sortBy = function(arr, fn) {
  return arr.sort((a, b) => fn(a) - fn(b));
};
```

📌 **Explanation:**

- This approach is valid when `fn(a)` and `fn(b)` return **numeric values**.
- Subtracting `fn(a) - fn(b)` directly gives:
  - Negative → `a` comes before `b`
  - Positive → `b` comes before `a`
  - Zero → keep original order

✅ **Pros:**
- Very concise and elegant
- Ideal for numeric transformations

❗ **Note:** Not ideal if `fn()` returns strings or complex types.

---

# 🔹 **Approach 2: Boolean-Based Comparator**

```js
var sortBy = function(arr, fn) {
  return arr.sort((a, b) => fn(a) > fn(b) ? 1 : -1);
};
```

📌 **Explanation:**

- Compares `fn(a)` and `fn(b)` using `>` operator
- If `fn(a)` is greater → returns `1` (a goes after b)
- Otherwise → returns `-1` (a goes before b)

✅ **Pros:**
- Works well for **strings, booleans**, and other comparable values
- Safer in some non-numeric cases

❗ **Note:** This does not handle **equality cases** explicitly.

---

# **Complexity Analysis**

- **Time Complexity:**  
  - $$O(n \log n)$$ – Standard time for array sorting
- **Space Complexity:**  
  - $$O(1)$$ – In-place sorting, no extra space required

---

# **Code Implementation**

```js
/**
 * @param {Array} arr
 * @param {Function} fn
 * @return {Array}
 */

var sortBy = function(arr, fn) {
  return arr.sort((a, b) => fn(a) > fn(b) ? 1 : -1);
};
```

---

# **Important Topics to Learn 📚**

- **Array.prototype.sort()**: How JavaScript sorts arrays under the hood  
- **Custom Comparator Functions**: Writing flexible sorting logic  
- **Higher-Order Functions**: Passing behavior through callbacks  
- **Stable Sorts**: Maintaining order when values are equal  
- **Arrow Functions**: Simplifying function syntax

---

# 🚀 **Support & Feedback**

✅ If you found this helpful, **drop an upvote & share your thoughts**!  
💬 Let’s discuss alternate ways to solve this or make it more generic! 👇

--- 
['JavaScript']