In this tutorial, we will be using a function to swap array elements in JavaScript/jQuery. We will implement the same objective, using an Array.prototype.
Hence, let's start with the code.
Method 1
Check the code, mentioned below. Here, I have used jQuery version 3.1.0. Inside the $(document).ready() section, we have declared an array and a Swap() function to swap the array elements. We are passing the actual array and the indexes to this function.
  1. <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function(){
  4. var myArray = [18,3,90,25,2,27,36, 22, 4]; //Declare the array
  5. var testString = ''; // declare a temporary variable to store the sorted array values.
  6. //Declare the function to swap the array elements using a third variable.
  7. //Here we are passing the array indexes as well as the array itself
  8. function Swap(arr,a, b){
  9. var temp;
  10. temp = arr[a];
  11. arr[a] = arr[b];
  12. arr[b] = temp;
  13. }
  14. //Declare the function to sort the array
  15. function Sort(array){
  16. for(var j = 0; j< array.length-1; j++){
  17. for(var k = 1; k< array.length; k++){
  18. if(array[k] < array[k - 1]){
  19. Swap(array,k,k-1) //call the Swap function here
  20. }
  21. }
  22. }
  23. }
  24. //Call the Sort Function with the array declared above
  25. Sort(myArray);
  26. //Loop through the sorted array elements after swapping
  27. for(var i = 0; i < myArray.length; i++){
  28. testString += ", " + myArray[i];
  29. }
  30. alert(testString.substring(2)); // Display the sorted array after swapping
  31. });
  32. </script>
Method 2
In this method, we will implement the Swap() function with the Array.prototype builtin. Check with the code, mentioned below. I have added Swap() function to Array.prototype.
P.S
This method should be avoided while working with multiple JS library, as it may create confusion.
  1. <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function(){
  4. var myArray = [18,3,90,25,2,27,36, 22, 4];
  5. var testString = '';
  6. //Declare the Swap function with Array.prototype. We are only passing the indexes here.
  7. Array.prototype.Swap = function (x,y) {
  8. var b = this[x];
  9. this[x] = this[y];
  10. this[y] = b;
  11. return this; // return the current array instance.
  12. }
  13. //Declare the sort function
  14. function Sort(array){
  15. for(var j = 0; j< array.length-1; j++){
  16. for(var k = 1; k< array.length; k++){
  17. if(array[k] < array[k - 1]){
  18. array.Swap(k,k-1) // Call the Swap() function like this.
  19. }
  20. }
  21. }
  22. }
  23. //call the Sort() function
  24. Sort(myArray);
  25. for(var i = 0; i < myArray.length; i++){
  26. testString += ", " + myArray[i];
  27. }
  28. alert(testString.substring(2)); // display the sorted array.
  29. });
  30. </script>
I hope, this will be helpful for the beginners in JavaScript. Please comment and provide the feedback, if you like my post. Thank you for reading.