## DEV Community

Nelson Chibukasia

Posted on • Updated on

# Find max and min values of an array using sort with compare functions

Arrays in JavaScript do not have inbuilt methods to find the maximum and minimum numbers. However, there are some other ways to achieve that. The easiest way is to use the Math methods, that is Math.max.apply(null, numberArray) for maximum and Math.min.apply(null, numberArray). However, in this post, we shall use another technique to achieve the same results; the sort method. We shall use the sort method to arrange the values in either ascending or descending order and use the index to get the minimum and maximum number.
Using the sort method
First, we shall arrange the numbers in the array using the sort method. The array sort method arranges elements in array in an ascending order. However, the sort method does not always work well with an array of numbers in JavaScript. It is only perfect with an arrays of strings. To use the sort method with array of numbers, we introduce the compare function. The compare function uses the format function (a, b){return a-b}. Combining with the sort method, we have something similar to this, numberArray.sort(function (a, b){return a-b}). This format return an array in ascending order.

``````//create an array of numbers
const numbers = [2, 67,86, 34, 67, 45]

//sort the arrays using the compare function
let sortedNumbers = numbers.sort(function(a, b){
return a-b;
})

console.log(sortedNumbers) //[ 2, 34, 45, 67, 67, 86 ]
``````

But how does this work? This might be a little bit confusing but pretty easy to understand. The sort function takes two numbers and sends them to the compare function. The compare function performs the evaluation on the two numbers and returns the result. The returned result can only appear in three forms, that is positive, negative, or zero. If the returned result is a negative number, the first value in the compare function, a for this case, is sorted before the second value,b Sort function then uses the returned value to sort the numbers. The vice-versa happens if the returned value is positive, b is sorted before a. For the return value zero, no changes are made to the sorting order of the two values.
Now, since we have a new array sorted in descending order, lets now get the min and maximum values. This is pretty easy to achieve since we only use the index value of the first element as the minimum and index of the last element as the maximum

``````//create an array of numbers
const numbers = [2, 67,86, 34, 67, 45]

//sort the arrays using the compare function
let sortedNumbers = numbers.sort(function(a, b){
return a-b;
})

console.log(sortedNumbers) //[ 2, 34, 45, 67, 67, 86 ]

//get the minimum value
const minimumValue = sortedNumbers[0];
console.log(minimumValue) //2

//get the maximum value
const maximumValue = sortedNumbers[sortedNumbers.length-1];
console.log(maximumValue) //86
``````

We can as well sort the array in a descending order and get the minimum and maximum value by interchanging the index values. To sort in a descending order, the compare function will have the return value as the result of subtracting the first value from the second value.

``````// //create an array of numbers
const numbers = [2, 67,86, 34, 67, 45]

//sort the arrays in descending order using the compare function
let sortedNumbersDesc = numbers.sort(function(a, b){
return b-a;
})

console.log(sortedNumbersDesc) //[ 2, 34, 45, 67, 67, 86 ]

//get the minimum value
const minimumValue = sortedNumbersDesc[sortedNumbersDesc.length-1];
console.log(minimumValue) //2

//get the maximum value
const maximumValue = sortedNumbersDesc[0];
console.log(maximumValue) //86
``````

Use Math.max.apply() and Math.min.appy()
Let's try and use the Math.max.apply() and Math.min.apply() to achieve the same.

``````// //create an array of numbers
const numbers = [2, 67,86, 34, 67, 45];
//get the minimum value
console.log(Math.min.apply(null, numbers)) // 2
//get the maximum value
console.log(Math.max.apply(null, numbers)) // 86
``````

That's it for this post. Hope you enjoyed reading and happy coding!

`Math.max(...numbers)`
`Math.min(...numbers)`