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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson