CFLib.org – Common Function Library Project

ListUnion(List1, List2[, Delim1][, Delim2][, Delim3][, SortType][, SortOrder])

Last updated November 14, 2001

author

Rob Brooks-Bilson

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Combines two lists, automatically removing duplicate values. Allows for optional delimiters for all lists. Also allows for optional sort type and sort order.

Return Values:
Returns a string.

Example:

<CFSET List1="20,13,15,18,12,19,11,13,13,14,20,21,11,14">
<CFSET List2="10|2|3|4|4|4|4|5|6|1|7|8|8|9|10">

<CFOUTPUT>
List1: #List1#<BR>
List2: #List2#<BR>
<P>
Union: #ListUnion(List1,List2, ",", "|", ":", "numeric", "asc")#
</CFOUTPUT>

Parameters:

Name Description Required
List1 First list of delimited values. Yes
List2 Second list of delimited values. Yes
Delim1 Delimiter used for List1. Default is the comma. No
Delim2 Delimiter used for List2. Default is the comma. No
Delim3 Delimiter to use for the list returned by the function. Default is the comma. No
SortType Type of sort: Text or Numeric. The default is Text. No
SortOrder Asc for ascending, DESC for descending. Default is Asc No

Full UDF Source:

/**
 * Combines two lists, automatically removing duplicate values and returning a sorted list.
 * 
 * @param List1 	 First list of delimited values. 
 * @param List2 	 Second list of delimited values. 
 * @param Delim1 	 Delimiter used for List1.  Default is the comma. 
 * @param Delim2 	 Delimiter used for List2.  Default is the comma. 
 * @param Delim3 	 Delimiter to use for the list returned by the function.  Default is the comma. 
 * @param SortType 	 Type of sort:  Text or Numeric.  The default is Text. 
 * @param SortOrder 	 Asc for ascending, DESC for descending.  Default is Asc 
 * @return Returns a string. 
 * @author Rob Brooks-Bilson (rbils@amkor.com) 
 * @version 1, November 14, 2001 
 */
function ListUnion(List1, List2)
{
  var TempList = "";
  var CombinedList = "";  
  Var SortType="text";
  Var SortOrder="asc";
  var Delim1 = ",";
  var Delim2 = ",";
  var Delim3 = ",";
  var i = 0;
  // Handle optional arguments
  switch(ArrayLen(arguments)) {
    case 3:
      {
        Delim1    = Arguments[3];
        break;
      }
    case 4:
      {
        Delim1    = Arguments[3];
        Delim2    = Arguments[4];        
        break;
      }
    case 5:
      {
        Delim1    = Arguments[3];
        Delim2    = Arguments[4];        
        Delim3    = Arguments[5];  
        break;
      }       
    case 6:
      {
        Delim1    = Arguments[3];
        Delim2    = Arguments[4];        
        Delim3    = Arguments[5];  
        SortType  = Arguments[6];
        break;
      }       
    case 7:
      {
        Delim1    = Arguments[3];
        Delim2    = Arguments[4];        
        Delim3    = Arguments[5];  
        SortType  = Arguments[6];
        SortOrder = Arguments[7];
        break;
      }                    
  } 
  
  // Combine list 1 and list 2 with the proper delimiter
  CombinedList = ListChangeDelims(List1, Delim3, Delim1) & Delim3 &  ListChangeDelims(List2, Delim3, Delim2);
  // Strip duplicates if indicated
  for (i=1; i LTE ListLen(CombinedList, Delim3); i=i+1) {
    if (NOT ListFindNoCase(TempList, ListGetAt(CombinedList, i, Delim3), Delim3)){
     TempList = ListAppend(TempList, ListGetAt(CombinedList, i, Delim3), Delim3);
    }
  }
  Return ListSort(TempList, SortType, SortOrder, Delim3);
}
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