I was wondering whether ColdFusion uses any binary search algorithm for searching? Does anyone of you have any idea on in-built functions and what they use?
Asked
Active
Viewed 119 times
0
-
I would use the `find()` or `filter()` and then see how they scale. OR I would go to Lucee and dive into its source code to see how they implemented it. – James A Mohler Aug 04 '20 at 04:19
-
@JamesAMohler - Thank you for your response. find() will be linear and I wanted to use the binary search algorithm. I had written my own function, however, wondering how ColdFusion does it and how come ColdFusion doesn't have any inbuilt function for this. I have no idea about Lucee, could you guide me through it so that I would like to check it once? Thanks! – Chaitu Aug 04 '20 at 20:57
-
I don't know Lucee well enough to find. You may want to consider putting in an issue with https://tracker.adobe.com/ maybe they will build something someday, or at least explain why they don't have it. – James A Mohler Aug 04 '20 at 21:37
-
1If you really think it is needed, you could also look into using java. Its Collections lib offers binary search functions. – SOS Aug 04 '20 at 23:12
-
Thank you for your suggestions. Appreciated. – Chaitu Aug 05 '20 at 21:40
1 Answers
1
Coldfusion lets you use anything in the Java standard library.
<cfset arr = [1, 2, 3, 4]>
<cfset collections = createObject('java', 'java.util.Collections')>
<cfdump var="#collections.binarySearch(arr, 3)#">
Keep in mind that the returned value is zero-indexed, while CF is one-indexed.
You may need to do more complicated conversions depending on the data types inside your array, and of course the array must be sorted before you use binarySearch.

Tim Jasko
- 1,532
- 1
- 8
- 12
-
1@JamesAMohler - Sometimes, yes. Java's binarySearch is more picky than CF. Array elements must be of the same type of you get a ClassCastException. Also, **the array must be sorted** or the results are undefined. – SOS Aug 05 '20 at 21:41
-
Is there a way to look at the built-in function(s) code implementation just like Java? – Chaitu Aug 05 '20 at 22:21
-
Lucee yes. Because it is open source. Adobe CF, no. Unless you work for Adobe. – James A Mohler Aug 05 '20 at 23:03
-
Tim, may want to mention arrays *must* be sorted first, or it won't work as expected. – SOS Aug 06 '20 at 16:35
-
No conversions are necessary in this case; the above code runs fine in Adobe CF. – Tim Jasko Aug 11 '20 at 19:58