Array Sort Methods
sort() is an array method used to arrange the elements of an array in a specific order. By default, it sorts elements as strings in ascending order. We can also provide a compare function to sort numbers or custom data in ascending or descending order. This method modifies the original array and returns the sorted array. The return type is Array. It is useful when we need to organize data in a particular order.
let fruits = ["Mango", "Apple", "Orange"];
let result = fruits.sort();//["Apple", "Mango", "Orange"]
Array sort()
sort() is an array method used to arrange the elements of an array in a specific order. By default, it sorts elements as strings in ascending order. It modifies the original array and returns the sorted array. The return type is Array. We can also use a compare function to sort numbers or custom data in ascending or descending order.
let fruits = ["Mango", "Apple", "Orange"];
console.log(fruits.sort());//[ 'Apple', 'Mango', 'Orange' ]
Array reverse()
reverse() is an array method used to reverse the order of elements in an array. It changes the original array by reversing the sequence of elements. The return type is Array. It is useful when we want to display or process data in reverse order.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits.reverse());//[ 'Orange', 'Mango', 'Apple' ]
Array toSorted()
toSorted() is an array method used to sort the elements of an array in ascending or descending order without modifying the original array. It works similar to sort(), but instead of changing the original array, it returns a new sorted array. The return type is Array. It is useful when we want to keep the original data unchanged while sorting.
let fruits = ["Mango", "Apple", "Orange"];
let result = fruits.toSorted();
console.log(result);//[ 'Apple', 'Mango', 'Orange' ]
console.log(fruits);//[ 'Mango', 'Apple', 'Orange' ]
Array toReversed()
toReversed() is an array method used to reverse the order of elements in an array without modifying the original array. It returns a new array with elements in reverse order. The return type is Array. It is useful when we want a reversed version of the array while keeping the original array unchanged.
let fruits = ["Apple", "Mango", "Orange"];
let result = fruits.toReversed();
console.log(result);//[ 'Orange', 'Mango', 'Apple' ]
console.log(fruits);//[ 'Apple', 'Mango', 'Orange' ]
Sorting Objects
We can sort objects in JavaScript using the sort() method along with a compare function. Since objects do not have a default sorting order, we need to specify a property based on which the sorting should be done, such as name, age, or marks. The compare function helps decide the order of objects by comparing their property values.
let students = [
{ name: "A", age: 25 },
{ name: "B", age: 20 },
{ name: "C", age: 22 }
];
students.sort((a, b) => a.age - b.age);
console.log(students);
//[
{ name: 'B', age: 20 },
{ name: 'C', age: 22 },
{ name: 'A', age: 25 }
]
Numeric Sort
Numeric sort in JavaScript is used to sort numbers in ascending or descending order using the sort() method with a compare function. By default, sort() treats numbers as strings, so we need a compare function to correctly sort numeric values.It modifies the original array and returns the sorted array.
let nums = [30, 5, 100, 20];
nums.sort((a, b) => a - b);
console.log(nums);//[ 5, 20, 30, 100 ]
let nums = [30, 5, 100, 20];
nums.sort((a, b) => b - a);
console.log(nums);//[ 100, 30, 20, 5 ]
Math.min()
Math.min() is a built-in JavaScript method used to find the smallest value among the given numbers. It takes one or more numeric arguments and returns the minimum value. The return type is Number. It is useful when we need to identify the smallest value from a set of numbers.
console.log(Math.min(10, 5, 20, 3));//3
let nums = [10, 5, 20, 3];
console.log(Math.min(...nums));//3
Math.max()
Math.max() is a built-in JavaScript method used to find the largest value among the given numbers. It takes one or more numeric arguments and returns the maximum value. The return type is Number. It is useful when we need to identify the largest value from a set of numbers.
console.log(Math.max(10, 5, 20, 3));//20
let nums = [10, 5, 20, 3];
console.log(Math.max(...nums));//20
Home made Min()
We can create a custom min() function by iterating through the array and comparing each element. We assume the first element as the minimum and update it whenever we find a smaller value. Finally, we return the smallest value. The return type is Number.
function myMin(arr) {
let min = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
let nums = [10, 5, 20, 3];
console.log(myMin(nums));//3
Home made Max()
We can create a custom max() function by iterating through the array and comparing each element. We assume the first element as the maximum and update it whenever we find a larger value. Finally, we return the largest value. The return type is Number.
function myMax(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
let nums = [10, 5, 20, 3];
console.log(myMax(nums));//20
Top comments (0)