1. Numeric Sort
Numeric sorting arranges numbers in ascending or descending order.
JavaScript's default sort() method treats values as strings, which can produce unexpected results.
Problem with Default sort()
const numbers = [100, 25, 8, 50];
numbers.sort();
console.log(numbers);
Output
[100, 25, 50, 8]
This happens because JavaScript converts numbers into strings and performs lexicographic (dictionary) sorting.
JavaScript compares:
"100"
"25"
"50"
"8"
instead of actual numeric values.
Numeric Sort (Ascending)
const numbers = [100, 25, 8, 50];
numbers.sort((a, b) => a - b);
console.log(numbers);
Output
[8, 25, 50, 100]
How It Works
a - b
- Negative →
acomes beforeb - Positive →
bcomes beforea - Zero → no change
Numeric Sort (Descending)
const numbers = [100, 25, 8, 50];
numbers.sort((a, b) => b - a);
console.log(numbers);
Output
[100, 50, 25, 8]
Real-World Example
const marks = [78, 95, 66, 82, 99];
marks.sort((a, b) => b - a);
console.log(marks);
Output
[99, 95, 82, 78, 66]
Useful for:
- Ranking students
- Sorting scores
- Sorting prices
- Sorting ages
2. Random Sort
Random sorting is commonly used to shuffle array elements.
Example
const numbers = [1, 2, 3, 4, 5];
numbers.sort(() => Math.random() - 0.5);
console.log(numbers);
Possible Output
[3, 1, 5, 2, 4]
Another run may produce:
[5, 2, 1, 4, 3]
How It Works
Math.random()
returns a value between:
0 and 1
Example:
0.15
0.87
0.43
Subtracting 0.5 creates positive and negative values:
Math.random() - 0.5
Possible results:
-0.35
0.27
-0.12
These random positive and negative values cause sort() to rearrange elements unpredictably.
Use Cases
- Quiz applications
- Card games
- Random questions
- Random item selection
- Entertainment apps
3. Math.min()
Math.min() returns the smallest value from a list of numbers.
Syntax
Math.min(value1, value2, value3, ...)
Example
console.log(
Math.min(10, 20, 5, 30)
);
Output
5
Using Math.min() with Arrays
Arrays must be expanded using the spread operator.
const numbers = [10, 20, 5, 30];
console.log(
Math.min(...numbers)
);
Output
5
How Spread Operator Works
...numbers
converts:
[10, 20, 5, 30]
into:
10, 20, 5, 30
Therefore JavaScript sees:
Math.min(10, 20, 5, 30)
Real-World Example
const temperatures =
[32, 28, 25, 30, 35];
console.log(
Math.min(...temperatures)
);
Output:
25
4. Math.max()
Math.max() returns the largest value from a list of numbers.
Syntax
Math.max(value1, value2, value3, ...)
Example
console.log(
Math.max(10, 20, 5, 30)
);
Output
30
Using Math.max() with Arrays
const numbers = [10, 20, 5, 30];
console.log(
Math.max(...numbers)
);
Output
30
Real-World Example
const sales =
[5000, 8500, 3000, 12000];
console.log(
Math.max(...sales)
);
Output:
12000
Useful for finding:
- Highest salary
- Highest marks
- Highest sales
- Highest temperature
5. Home-Made Min()
Interviewers often ask:
Find the minimum value without using Math.min().
This helps test problem-solving skills and loop understanding.
Logic
- Assume first element is minimum.
- Compare remaining elements.
- Update minimum whenever a smaller value is found.
Example
const numbers = [10, 11, 1, 100, 89];
let min = numbers[0];
for(let i = 1; i < numbers.length; i++){
if(numbers[i] < min){
min = numbers[i];
}
}
console.log(min);
Output
1
Dry Run
Array:
[10, 11, 1, 100, 89]
Initially:
min = 10
Compare:
11 < 10
False
Compare:
1 < 10
True
Update:
min = 1
Remaining values are larger.
Final answer:
1
Real-World Use Case
const productPrices =
[250, 500, 120, 900, 300];
Finding the cheapest product.
6. Home-Made Max()
Another common interview question:
Find the maximum value without using Math.max().
Logic
- Assume first element is maximum.
- Compare remaining elements.
- Update maximum whenever a larger value is found.
Example
const numbers = [10, 11, 1, 100, 89];
let max = numbers[0];
for(let i = 1; i < numbers.length; i++){
if(numbers[i] > max){
max = numbers[i];
}
}
console.log(max);
Output
100
Dry Run
Array:
[10, 11, 1, 100, 89]
Initially:
max = 10
Compare:
11 > 10
True
Update:
max = 11
Compare:
1 > 11
False
Compare:
100 > 11
True
Update:
max = 100
Final answer:
100
Real-World Use Case
const salaries =
[25000, 40000, 55000, 32000];
Finding the highest salary.
References:
https://www.w3schools.com/js/js_array_sort.asp
https://www.geeksforgeeks.org/javascript/javascript-array-methods/
Top comments (0)