<cfsilent>
<cfparam name="ATTRIBUTES.sort" type="struct">
<cfparam name="ATTRIBUTES.fieldcount" type="numeric" default="0">
<cfparam name="ATTRIBUTES.sortlength" type="numeric" default="1">

<cfif NOT isDefined("ATTRIBUTES.sort.sortArray")>
	<cfset structInsert(ATTRIBUTES.sort,"sortArray",arrayNew(1),"TRUE")>
</cfif>
<cfset sortArray=ATTRIBUTES.sort.sortArray>

<cfif evaluate("isDefined('CALLER.sort')")>
	<cfset evaluate("val=(CALLER.sort)")>
	<!--- parse --->	
	<cfif val EQ "reset">
		<cfset ATTRIBUTES.sort.sortArray=arrayNew(1)>
		<cfexit method="exittag"/>
	</cfif>
	<cfset pos=find("a",val)>
	<cfif pos LE 0>
		<cfset pos=find("d",val)>
	</cfif>
	
	<cfif pos LE 1>
		<cfexit>
	<cfelseif pos GT 1>
		<cfset fldNum=left(val,pos-1)>
		<cfset fldAsc=mid(val,pos,1)>
	<cfelse>
		<cfset fldNum=val>
		<cfset fldAsc="a"> 
	</cfif>
	<!---/parse --->
	<!--- validate --->
	<cfif NOT isNumeric(fldNum)>
		<cfexit>
	</cfif>
	<cfif (ATTRIBUTES.fieldcount GT 0) AND (fldNum GT ATTRIBUTES.fieldcount)>
		<cfexit>
	</cfif>
	<!---/validate --->
<cfelse>
	<cfexit method="exittag"/>
</cfif>

<cfset sortItem=structNew()>
<cfset structInsert(sortItem,"fld",fldNum)>
<cfset structInsert(sortItem,"asc",(fldAsc EQ "a"))>

<!--- drop all occurences of this field --->
<cfset i=1>
<cfloop condition="i LE arrayLen(sortArray)">
	<cftry>
		<cfif sortArray[i].fld EQ sortItem.fld>
			<cfset arrayDeleteAt(sortArray,i)>
		</cfif>
	<cfcatch type="Any">
		<cfset arrayDeleteAt(sortArray,i)>
	</cfcatch>
	</cftry>
	<cfset i=i+1>
</cfloop>

<!--- insert item --->
<cfset arrayPrepend(sortArray, sortItem)>

<!--- truncate array--->
<cfloop condition="arrayLen(sortArray) GT ATTRIBUTES.sortlength">
	<cfset arrayDeleteAt(sortArray,arrayLen(sortArray))>
</cfloop> 

<!--- save --->
<cfset ATTRIBUTES.sort.sortArray=sortArray>
<!---/save --->

<cfexit method="exittag"/>
</cfsilent>