Guest User

Friday Puzzler - isSorted()

a guest
Mar 29th, 2013
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <cfset inputs = [
  2. "1,4,8,12,90",
  3. "99,88,70,3,-2",
  4. "Apple,Banana,Cherry,Donut,anna,berry,cherry",
  5. "anna,Apple,Banana,berry,cherry,Cherry",
  6. "Zed,Xymox,Joy Division,Cure",
  7. "Zed,zip,Xymox,xoom",
  8. "apple,banana,aardvark",
  9. "9,2,9,1,99,-21",
  10. "",
  11. "1,2",
  12. "2,1",
  13. "A"
  14.  
  15. ]>
  16.  
  17. <cfoutput>
  18. <cfloop array="#inputs#" index="list">
  19.     <cfdump var="#isSorted(list)#" label="#list#">
  20. </cfloop>
  21.  
  22. </cfoutput>
  23.  
  24. <cffunction name="isSorted" returntype="struct" >
  25.     <cfargument name="listData" required="true" type="string" />
  26.    
  27.     <cfset local.result = structNew() />
  28.     <cfset local.result.cInsTxtSortedAsc = "NO"/>
  29.     <cfset local.result.cInsTxtSortedDesc = "NO"/>
  30.     <cfset local.result.cSensTxtSortedAsc = "NO"/>
  31.     <cfset local.result.cSensTxtSortedDesc = "NO"/>
  32.     <cfset local.result.numSortedAsc = "NO"/>
  33.     <cfset local.result.numSortedDesc = "NO"/>
  34.     <cfset local.result.isSorted = "NO" />
  35.    
  36.     <cftry>
  37.     <cfif listLen(arguments.listData) GT 1>
  38.         <cfif reFind("[^0-9,]", arguments.listData)>
  39.             <cfset local.result.cInsTxtSortedAsc = compareNoCase(listSort(arguments.listData, "textnocase", "asc" ), arguments.listData) eq 0 />
  40.             <cfset local.result.cInsTxtSortedDesc = compareNoCase(listSort(arguments.listData, "textnocase", "desc" ), arguments.listData) eq 0 />
  41.             <cfset local.result.cSensTxtSortedAsc = compare(listSort(arguments.listData, "text", "asc" ), arguments.listData) eq 0 />
  42.             <cfset local.result.cSensTxtSortedDesc = compare(listSort(arguments.listData, "text", "desc" ), arguments.listData) eq 0 />
  43.         <cfelse>
  44.             <cfset local.result.numSortedAsc = compare(listSort(arguments.listData, "numeric", "asc" ), arguments.listData) eq 0 />
  45.             <cfset local.result.numSortedDesc = compare(listSort(arguments.listData, "numeric", "desc" ), arguments.listData) eq 0 />
  46.         </cfif>
  47.        
  48.         <cfif local.result.cInsTxtSortedAsc OR local.result.cInsTxtSortedDesc OR local.result.cSensTxtSortedAsc OR local.result.cSensTxtSortedDesc
  49.                 OR local.result.numSortedAsc OR local.result.numSortedDesc>
  50.             <cfset local.result.isSorted = "YES" />
  51.         </cfif>
  52.        
  53.     <cfelseif listLen(arguments.listData) EQ 1>
  54.         <!--- Empty or 1 element, that's in order  --->
  55.        
  56.         <cfif reFind("[^0-9,]", arguments.listData)>
  57.             <cfset local.result.cInsTxtSortedAsc = "YES"/>
  58.             <cfset local.result.cInsTxtSortedDesc = "YES"/>
  59.             <cfset local.result.cSensTxtSortedAsc = "YES"/>
  60.             <cfset local.result.cSensTxtSortedDesc = "YES"/>
  61.             <cfset local.result.numSortedAsc = "NO"/>
  62.             <cfset local.result.numSortedDesc = "NO"/>
  63.             <cfset local.result.isSorted = "YES" />
  64.         <cfelse>
  65.             <cfset local.result.cInsTxtSortedAsc = "NO"/>
  66.             <cfset local.result.cInsTxtSortedDesc = "NO"/>
  67.             <cfset local.result.cSensTxtSortedAsc = "NO"/>
  68.             <cfset local.result.cSensTxtSortedDesc = "NO"/>
  69.             <cfset local.result.numSortedAsc = "YES"/>
  70.             <cfset local.result.numSortedDesc = "YES"/>
  71.             <cfset local.result.isSorted = "YES" />
  72.         </cfif>
  73.     <cfelse>
  74.         <cfset local.result.cInsTxtSortedAsc = "YES"/>
  75.         <cfset local.result.cInsTxtSortedDesc = "YES"/>
  76.         <cfset local.result.cSensTxtSortedAsc = "YES"/>
  77.         <cfset local.result.cSensTxtSortedDesc = "YES"/>
  78.         <cfset local.result.numSortedAsc = "YES"/>
  79.         <cfset local.result.numSortedDesc = "YES"/>
  80.         <cfset local.result.isSorted = "YES" />
  81.     </cfif>
  82.     <cfcatch type="any">
  83.     <cfdump var="#cfcatch#">
  84.     </cfcatch>
  85.     </cftry>
  86.     <cfreturn local.result />
  87. </cffunction>
RAW Paste Data