Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




sort() - FUNCTION


The sort() Function is used to sort a Array in Ascending/Descending order.


Sorting a Array in Ascending order with Strings


Example :



<html>
<body>  
<script>  

	var x = ["Mohan", "Kriti", "Salim"] 
	x.sort()
	document.write("The Sorted Array in ascending order is [",x,"]") 
    
</script>      
</body>
</html>


Output :



  The Sorted Array in ascending order is [Kriti, Mohan, Salim]

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Then we have used the sort() method to sort the Array x in ascending order.


x.sort()

And the Array x gets sorted with Kriti as the first value, Mohan second and Salim as the third.

java_Collections

And we get the below output.


The Sorted Array in ascending order is [Kriti, Mohan, Salim]

Sorting a Array in Descending order with Strings


Example :



<html>
<body>  
<script>

	var x = ["Mohan", "Kriti", "Salim"] 
	x.sort().reverse()
	document.write("The Sorted Array in ascending order is [",x,"]") 
    
</script>      
</body>
</html>


Output :



  The Sorted Array in descending order is [Salim, Mohan, Kriti]

So, in the above code we have created a Array and initialised to the variable x.


var x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Then we have used the parameter reverse = true along with the sort() method to sort the Array x in descending order.


x.sort().reverse()

And the Array x gets sorted in descending order with Salim as the first value, Mohan second and Kriti as the third.

java_Collections

And we get the below output.


The Sorted Array in descending order is [Salim, Mohan, Kriti]

How to sort an Array with numbers in Increasing/Deceasing Order?


Even here the sort() Function is used to sort a Array with numbers in Increasing/Deceasing Order.


But we need something else with the sort() Function. And why is that so?


Let us clear it with the below example.


Example :



<html>
<body>  
<script>

	var x = [57, 200, 313, 40] 
	x.sort()
	document.write("The Sorted Array in increasing order is [",x,"]")
    
</script>      
</body>
</html>


Output :



  The Sorted Array in increasing order is [200,313,40,57]

So, if you see the above output. We didn't get the array in sorted order.


This is because the sort() function works perfect for sorting a String Array. But not with an Integer Array.


And to use it with Integer Array, we need something called compare function. Compare function can be used with sort() function to sort an Integer Array.


Let us see in the below example.


Example :



<html>
<body>  
<script>

	var x = [57, 200, 313, 40] 
	x.sort(function(a, b){return a - b})
	document.write("The Sorted Array in increasing order is [",x,"]")
    
</script>      
</body>
</html>


Output :



  The Sorted Array in increasing order is [40,57,200,313]

Now, if you see the above output. You can see the Integer Array is sorted.


And it happened with the compare function that we have used with sort() function.


x.sort(function(a, b){return a - b})

All we have done is passed the compare function as an argument to the sort() function,


function(a, b){return a - b}

And the compare function takes care of the sorting.


We will be learning compare function in the next tutorial. For now, just remember the compare function looks like,


function(a, b){return a - b}

And for sorting, just pass the compare function to the argument of sort() function.


x.sort(function(a, b){return a - b})

Just that if you want to sort the numbers in ascending order, the return statement should be,


return a - b

And if you want to sort the numbers in descending order, just change the return statement to,


return b - a

Now that we have talked about sorting the numbers in descending order. Let us see it in the below example.


Example :



<html>
<body>  
<script>

	var x = [57, 200, 313, 40] 
	x.sort(function(a, b){return b - a})
	document.write("The Sorted Array in decreasing order is [",x,"]")
    
</script>      
</body>
</html>


Output :



  The Sorted Array in decreasing order is [313,200,57,40]