arraySwap

Swaps array values of an array at specified positions. This
function is more efficient than multiple cfset tags

arraySwap(array, position1, position2) → returns boolean

Member Function Syntax

someArray.swap(position1, position2)

Argument Reference for the arraySwap function

array

Required: Yes
The array in which postions will be swapped

position1

Required: Yes
Positon of 1st element to swap.

position2

Required: Yes
Postion of 2nd element to swap.

Examples sample code invoking the arraySwap function


Swap the position of two values in an array

superiorArray = ['Spider-Man','Green Goblin','Doctor Octopus','Venom'];
arraySwap(superiorArray,1,3);
writeDump(superiorArray);

Expected Result: ['Doctor Octopus', 'Green Goblin', 'Spider-Man', 'Venom']


Swap the position of two values in an array using the member function

CF 11+ or Lucee 4.5+

superiorArray = ['Spider-Man','Green Goblin','Doctor Octopus','Venom'];
superiorArray.swap(1,3);
writeDump(superiorArray);

Expected Result: ['Doctor Octopus', 'Green Goblin', 'Spider-Man', 'Venom']


Fork me on GitHub