Working with big data sets will require you to sort data. So, if you’re writing code for an application that needs to manage a lot of data, you should be writing codes to sort the data.
Again, when manipulating or arranging data in a specific order, JavaScript developers have the JavaScript array sort() function at their disposal. However, this will save you the stress of writing long codes to manipulate such data sets.
Read on to learn more about JavaScript array sorting.
What Exactly is Array Sorting?
The term “array sorting” describes the placement of data sets at evenly spaced addresses in computer memory in numerical, alphabetical, or other types of order. It is a statistical process that employs a specific approach to provide users with data in the most accessible manner possible.
Additionally, you can use the JavaScript array sort() function to arrange array elements in ascending or descending order.
For example:
// this array is alphabetically sorted by the sort() method.
const home_gadgets = ["washing machine", "oven", "refrigerator","television", "fan", "bed", "iron", "cupboard", "cooker", "cushion", "table"];
// call the sort() to action in the next line of code
home_gadgets.sort();
console.log(home_gadgets); // output........ (11) ['bed', 'cooker', 'cupboard', 'cushion', ' fan', 'iron', 'oven', 'refrigerator', 'table', ' television ', 'washing machine']
You have the option to decide to reverse an array after sorting it. However, you can do this with the reverse() method. And you can employ it to arrange an array’s elements in ascending order:
For example,
const home_gadgets = ["machine", "oven", "refrigerator","television ", "fan", "bed", "iron", "cupboard"];
// lets us sort the data set first before reversing
let rev_arr = home_gadgets.sort();
console.log(rev_arr); // output ........ (8) ['bed', 'cupboard', 'fan', 'iron', 'machine', 'oven', 'refrigerator', 'television ']
// call the reverse code function
rev_arr.reverse()
console.log(rev_arr); // output ........ (8) ['television ', 'refrigerator', 'oven', 'machine', 'iron', 'fan', 'cupboard', 'bed']
JavaScript Compare Function When Sorting Numbers as Strings
The sort() method will produce incorrect results when sorting numbers in string format. For example, when you have the numbers “25” and “100” in an array, the array sort will sort “25” to be larger than “100,” which is not accurate in this situation.
A compare function will correct this confusion. Also, the compare function’s goal is to specify a different sort order. Depending on the arguments, the compare function should return a negative, zero, or positive value; function(a, b)return a – b.
Again, you don’t need the compare function if the array of strings is empty because the sort method alone treats numbers as strings. But to change the behavior of the sort method for the array of numbers, you need to use the compare function.
Example
const arr_scores = [60, 102, 12, 51, 35, 10,2,7,9,3,56,7,9,99];
//let's try sorting these numbers with the sort function first
arr_scores.sort()
console.log(arr_scores); // output for sort function/....... (14) [10, 102, 12, 2, 3, 35, 51, 56, 60, 7, 7, 9, 9, 99]
// from the output, you can see that the sort function was unable to sort the array correctly
//let's try the compare function
arr_scores.sort(function(i, j){return i - j});
console.log(arr_scores); //output........ (14) [2, 3, 7, 7, 9, 9, 10, 12, 35, 51, 56, 60, 99, 102]
How to Find the Highest or Lowest Value in An Array
There are no built-in functions for finding the maximum or minimum value in an array.
However, you can use the index to find the highest and lowest values after sorting an array.
const arr_scores = [60, 102, 12, 51, 35, 10,2,7,9,3,56,7,9,99];
arr_scores.sort(function(i, j){return i - j});
console.log(arr_scores)
// The lowest value is now contained in points[0].
// and the highest value can be found at points[points.length-1].
If you only need to find the highest (or lowest) value in an array, sorting the entire array is a very time-consuming process. However, you can find the highest number in an array by using Math.max() on an Array:
Example
const arr_scores = [60, 102, 12, 51, 35, 10,2,7,9,3,56,7,9,99];
// this function finds the max of the array
function arr_scoreMax(arr) {
return Math.max.apply(null, arr);
}
console.log(arr_scoreMax(arr_scores)); // output.......102
// To locate the lowest value in an array, use Math.min.apply.
// this function finds the min of the array
function arr_scoreMin(arr) {
return Math.min.apply(null, arr);
}
console.log(arr_scoreMin(arr_scores)); // output.......2.
Conclusion
In this article, you learned about JavaScript array sorts and JavaScript compare functions.