Description:-

This function swaps the array values of an array at specified positions. This function is more efficient than multiple <cfset> tags.

arraySwap( array, position1, position2 );

The function operates in place and returns a boolean ( True or False ) to indicate the success or failure of the operation.

Member Function Syntax:-

array.swap( position1, position2 );

Attributes:-

array:- This is a required argument, the name of the array object for which positions will be swapped.

position1:- This is a required argument and it is a numeric field, the position of the first element to swap.

position2:- This is a required argument and it is a numeric field, the position of the second element to swap.

Example:-

<cfscript>
   marvelArray = [ "Captain America", "Black Widow", "Scarlet Witch", "Thor", "Black Panther", "Iron Man", "Spider-Man", "Vision", "Captain Marvel" ];

   writeOutput( "Old Array:-" );
   writeDump( marvelArray );

   arraySwap( marvelArray, 2, 8 );

   writeOutput( "Modified Array:-" );
   writeDump( marvelArray );
</cfscript>

Result:-

Old Array:-

array
1Captain America
2Black Widow
3Scarlet Witch
4Thor
5Black Panther
6Iron Man
7Spider-Man
8Vision
9Captain Marvel

Modified Array:-

array
1Captain America
2Vision
3Scarlet Witch
4Thor
5Black Panther
6Iron Man
7Spider-Man
8Black Widow
9Captain Marvel

Here we swap marvelArray array values “Black Widow” with “Vision”, whose positions are 2 and 8.