ArraySort2D()
Last updated October 8, 2002
Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib
Description:
Takes an array, sortcolumn value, sort type (numeric, text, textnocase), and optional sort order (defaults to asc) as arguments. Returns an array sorted by the sortcolumn in the second dimension according to the type and order.
Return Values:
Returns an array.
Example:
<cfset startArray[1][1] = 3>
<cfset startArray[1][2] = 5>
<cfset startArray[2][1] = 4>
<cfset startArray[2][2] = 2>
<cfset startArray[3][1] = 9>
<cfset startArray[3][2] = 1>
<cfoutput>
<cfloop from="1" to="#ArrayLen(startArray)#" index="i">
<cfloop from="1" to="#ArrayLen(startArray[i])#" index="j">
startArray[#i#][#j#] is #startArray[i][j]#<br> </cfloop>
</cfloop>
</p>
<cfset finalArray = #ArraySort2D(startArray, 2, "numeric")#>
<p>
<cfloop from="1" to="#ArrayLen(finalArray)#" index="i">
<cfloop from="1" to="#ArrayLen(finalArray[i])#" index="j">
finalArray[#i#][#j#] is #finalArray[i][j]#<br>
</cfloop>
</cfloop>
</p>
</cfoutput>
Parameters:
No arguments.
Full UDF Source:
<cfscript>
/**
* Sorts a two dimensional array by the specified column in the second dimension.
*
* @return Returns an array.
* @author Robert West (robert.west@digiphilic.com)
* @version 1, October 8, 2002
*/
function ArraySort2D(arrayToSort, sortColumn, type) {
var order = "asc";
var i = 1;
var j = 1;
var thePosition = "";
var theList = "";
var arrayToReturn = ArrayNew(2);
var sortArray = ArrayNew(1);
var counter = 1;
if (ArrayLen(Arguments) GT 3){
order = Arguments[4];
}
for (i=1; i LTE ArrayLen(arrayToSort); i=i+1) {
ArrayAppend(sortArray, arrayToSort[i][sortColumn]);
}
theList = ArrayToList(sortArray);
ArraySort(sortArray, type, order);
for (i=1; i LTE ArrayLen(sortArray); i=i+1) {
thePosition = ListFind(theList, sortArray[i]);
theList = ListDeleteAt(theList, thePosition);
for (j=1; j LTE ArrayLen(arrayToSort[thePosition]); j=j+1) {
arrayToReturn[counter][j] = arrayToSort[thePosition][j];
}
ArrayDeleteAt(arrayToSort, thePosition);
counter = counter + 1;
}
return arrayToReturn;
}
</cfscript>
Search CFLib.org
Latest Additions
Tony Felice added
writeStateSelect
13 hour(s) ago
Tony Felice added
varNameToText
14 hour(s) ago
Larry C. Lyons added
splitMX
14 hour(s) ago
Tony Felice added
listIsItemSimila...
14 hour(s) ago
Tony Felice added
listCountItemSim...
14 hour(s) ago
Top Rated
ListCompare
Rated 5.0, 6 time(s)
QueryStringChang...
Rated 5.0, 4 time(s)
FormatSSN
Rated 5.0, 2 time(s)
DollarAsString
Rated 5.0, 2 time(s)
CapFirstTitle
Rated 5.0, 2 time(s)