CFLib.org – Common Function Library Project

numTextListSort(list)

Last updated January 13, 2004

author

Matt

Version: 1 | Requires: CF6 | Library: StrLib

Description:
This function returns a sorted list that has numeric and text values starting with the numeric and ending with the text values.

Return Values:
Returns a string.

Example:

<cfscript>
myList='cash,car,12,5,4,3,2,1,11,10,9,8,7,6,home';
writeoutput(numTextListSort(myList));
</cfscript>

Parameters:

Name Description Required
list List to sort. Yes

Full UDF Source:

/**
 * This function is to be used, to sort a list that has numeric and text values.
 * 
 * @param list      List to sort. (Required)
 * @return Returns a string. 
 * @author Matt (Coldfusion@spiraldev.com) 
 * @version 1, January 12, 2004 
 */
function numTextListSort(list) {
    var numArray=arrayNew(1);
    var textArray=arrayNew(1);
    var mg = 1;
    var data = "";
    
    //convert to array for speed
    data = listToArray(list);
    
    //loop through the list passed to the function
    for(;mg lte arrayLen(data);mg=mg+1){
        //if the value at this position of the list is numeric put it in the numList List
        if(isNumeric(data[mg])) numArray[arrayLen(numArray)+1] = data[mg];
        //else put it in the textList List
        else textArray[arrayLen(textArray)+1] = data[mg];
    }
    
    //sort the numList
    arraySort(numArray,"numeric");
    //sort the textList
    arraySort(textArray,"text");
    //put together
    for(mg=1;mg lte arrayLen(textArray);mg=mg+1) {
        numArray[arrayLen(numArray)+1] = textArray[mg];
    }
    
    //return the mainList
    return arrayToList(numArray);
    
}

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