JavaScript Sorting Arrays

In JavaScript sorting arrays can be done in many ways. These ways are mentioned below:

Sorting an Array

The sort() the method is used to sort array elements in place. It changes the element’s place in the original array:

<!DOCTYPE html>
<html>
<body>

<script>
const language = ["Python", "R", "C++", "Ruby"];
document.write(language.sort());
</script>

</body>
</html>

Output:

JavaScript Sorting Arrays

Reversing an Array

The reverse() the method is used to reverse the elements in an array. To sort arrays in descending order this method can be used.

<!DOCTYPE html>
<html>
<body>

<script>
const language = ["Python", "R", "C++", "Ruby"];
document.write(language.reverse());
</script>

</body>
</html>

Output:

image 60

Finding Min and max Array Value

To find the min or max in an array there is no built-in function in JavaScript. For this purpose, we need to sort an array and then use the index to obtain the highest and lowest values. But this is a very inefficient method.

Sorting ascending:

<!DOCTYPE html>
<html>
<body>

<script>
const n = [34, 13, 24, 5, 23, 245, 8];
const s = n.sort(function(x, y){return x - y});
document.write(s);
</script>

</body>
</html>

Output:

image 65

Sorting descending:

<!DOCTYPE html>
<html>
<body>

<script>
const n = [34, 13, 24, 5, 23, 245, 8];
const s = n.sort(function(x, y){return y - x});
document.write(s);
</script>

</body>
</html>

Output:

image 64

Using Math.max() on an Array

We can use Math.max.apply for finding the highest number in an array:

<!DOCTYPE html>
<html>
<body>

<p>The maximum is:  <span id="tacode"></span>.</p>

<script>
const n= [410, 100, 46, 543, 235, 130];
document.getElementById("tacode").innerHTML = MaxArr(n);

function MaxArr(ar) {
  return Math.max.apply(null, ar);
}
</script>

</body>
</html>

Output:

image 66

Math.max.apply(null, [1, 2, 3]) is equivalent to Math.max(1, 2, 3).

Using Math.min() on an Array

We can use Math.min.apply for finding the lowest number in an array:

<!DOCTYPE html>
<html>
<body>

<p>The minimum is:  <span id="tacode"></span>.</p>

<script>
const n= [410, 100, 46, 543, 235, 130];
document.getElementById("tacode").innerHTML = MinArr(n);

function MinArr(ar) {
  return Math.min.apply(null, ar);
}
</script>

</body>
</html>

Output:

image 67

My Min / Max JavaScript Methods

The “homemade” method is the fastest solution to find max or min. In this method we loop through an array comparing each value with the highest value found:

<!DOCTYPE html>
<html>
<body>

<p>The maximum is:  <span id="tacode"></span>.</p>

<script>
const n= [410, 100, 46, 543, 235, 130];
document.getElementById("tacode").innerHTML = MaxArr(n);

function MaxArr(n) {
  let l = n.length;
  let m = -Infinity;
  while (l--) {
    if (n [l] > m) {
      m = n[l];
    }
  }
  return m;
}
</script>

</body>
</html>

Output:

image 66
<!DOCTYPE html>
<html>
<body>

<p>The minimum is <span id="tacode"></span>.</p>

<script>
const n = [410, 100, 46, 543, 235, 130];
document.getElementById("tacode").innerHTML = MinArr(n);

function MinArr(a) {
  let l = a.length;
  let m = Infinity;
  while (l--) {
    if (a[l] < m) {
      m = a[l];
    }
  }
  return m;
}
</script>

</body>
</html>

Output:

image 67

Numeric Sort

The above function is used to sort() string values. If we sort numbers using this method it will produce incorrect results. So, we will use compare function (define an alternative sort order) to fix this issue.

<!DOCTYPE html>
<html>
<body>

<script>
const n = [34, 13, 24, 5, 23, 245, 8];
const s = n.sort(function(x, y){return x - y});
document.write(s);
</script>

</body>
</html>

Output:

image 61

We can use the same trick to sort an array in descending order:

<!DOCTYPE html>
<html>
<body>

<script>
const n = [34, 13, 24, 5, 23, 245, 8];
const s = n.sort(function(x, y){return y - x});
document.write(s);
</script>

</body>
</html>

Output:

image 62

Random Order Sorting

The following method is used to sort an array in random order.

It produces random output every time.

JavaScript Sorting Arrays – The Fisher Yates Method

The above example is not able to work correctly, because it favors some numbers over others. To solve this issue, the Fisher-Yates shuffle method is used. It is used as follow:

It produces random output every time.