CFLib.org – Common Function Library Project

shiftArray(inArray, shiftOnValue)

Last updated August 16, 2005

author

Richard Nugen

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Allows you to move an array index and the remaining indexes below it to the top of an array. Extremly helpful in building grid based outputs such as calendars.

Return Values:
Returns an array.

Example:

<cfset dowArray = arrayNew(1)>
<cfloop index="x" from="1" to="7">
	<cfset arrayAppend(dowArray,x)>
</cfloop>

<cfloop index="x" from="1" to="7">
	<cfdump var="#shiftArray(dowArray,x)#">
</cfloop>

Parameters:

Name Description Required
inArray Array to shift. Yes
shiftOnValue Index to shift. Yes

Full UDF Source:

/**
 * Returns a shifted array at the passed Shift On value.
 * 
 * @param inArray 	 Array to shift. (Required)
 * @param shiftOnValue 	 Index to shift. (Required)
 * @return Returns an array. 
 * @author Richard Nugen (richard@corporatemedia.com) 
 * @version 1, August 16, 2005 
 */
function shiftArray(inArray,ShiftOnVal) {
	var tmpArray = arrayNew(1);
	var x = 0;
        
	for(x=1; x lte arrayLen(inArray); x=x+1) {
		if(inArray[x] EQ ShiftOnVal) { break; }
		else {
			arrayAppend(tmpArray,inArray[x]);
			arrayDeleteAt(inArray,x);
			x=0;
		}
	}

	for(x=1;x lte arrayLen(tmpArray); x=x+1) arrayAppend(inArray,tmpArray[x]);
        
	return inArray;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson