Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Sorting Arrays

JS Array Sorting

JavaScript array sorting arrange the array elements in ascending or descending order.

Array sort()

The sort() method sorts an array alphabetically

					 
        
          const services = ["Website", "Applications", "Software"];
          services.sort();  // ["Applications", "Software", "Website"]
        
      
Array reverse()

The reverse() method reverses the elements in an array. You can use it to sort an array in descending order.

					 
        
          const services1 = ["Website", "Applications", "Software"];
          services1.reverse();  // ["Software", "Applications", "Website"]

          // reverse sorting
          const services2 = ["Website", "Applications", "Software"];
          services2.sort();  // ["Applications", "Software", "Website"]
          services2.reverse();  // ["Website", "Software", "Applications"]
        
      
Numeric Sort

By default, the sort() function sorts values as strings.

This works well for strings ("Applications" comes before "Software").

However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".

Because of this, the sort() method will produce incorrect result when sorting numbers. You can fix this by providing a compare function.

					 
        
          // ascending order
          const points = [40, 100, 1, 5, 25, 10];
          points.sort(function(a, b){return a - b});

          // descending order
          const points = [40, 100, 1, 5, 25, 10];
          points.sort(function(a, b){return b - a});
        
      
Sorting an Array in Random Order
					 
        
          const points = [40, 100, 1, 5, 25, 10];
          points.sort(function(){return 0.5 - Math.random()});
        
      
Sorting By Fisher Yates Method

The above example, array.sort(), is not accurate. It will favor some numbers over the others. The most popular correct method, is called the Fisher Yates shuffle, and was introduced in data science as early as 1938.

					 
        
          const points = [40, 100, 1, 5, 25, 10];

          for (let i = points.length -1; i > 0; i--) {
            let j = Math.floor(Math.random() * (i+1));
            let k = points[i];
            points[i] = points[j];
            points[j] = k;
          }
        
      
Array Max and Min Value

There are no built-in functions for finding the max or min value in an array. However, after you have sorted an array, you can use the index to obtain the highest and lowest values.

					 
        
          const points = [40, 100, 1, 5, 25, 10];
          points.sort(function(a, b){return a - b});
          points[0]; // min value
          points[points.length - 1]; // max value
        
      
Using Math.max() And Math.min()

You can use Math.max.apply to find the highest number in an array.

You can use Math.min.apply to find the lowest number in an array.

					 
        
          const points = [40, 100, 1, 5, 25, 10];
          let max = Math.max.apply(null, points);  //  100
          let min = Math.min.apply(null, points);  //  1
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved