Two of the most common things you'll do with an array: find something in it, and put it in a specific order. ColdFusion has dedicated functions for both, rather than requiring a manual loop every time.
Learning Objectives
After completing this lesson, you'll be able to:
- Find the position of a value with arrayFind and arrayFindAll.
- Check whether a value exists with arrayContains.
- Check whether a specific index exists with arrayIndexExists.
- Sort an array by type (numeric or text) and order (ascending or descending).
- Sort an array of structs with a custom comparison function.
- Reverse an array's order with arrayReverse.
Finding a Value's Position: arrayFind
arrayFind returns 0 if the value isn't found — not -1, unlike some other languages. Use arrayFindNoCase for a case-insensitive match.
Finding Every Match: arrayFindAll
arrayFindAll returns an array of every matching index, not just the first.
Checking Existence: arrayContains
arrayContains actually returns the matching index (like arrayFind), which is truthy when found and 0 (falsy) when not — so it works fine in an if condition even though it isn't a strict Boolean.
Checking an Index Exists: arrayIndexExists
Useful before accessing an index that might not exist, to avoid an out-of-bounds error.
Sorting by Type: arraySort
The simplest form takes a sort type ("numeric", "text", or "textnocase") and an optional order ("asc", the default, or "desc").
This modifies the array in place, the same way the add/remove functions from the previous lesson do.
Sorting With a Custom Callback
For anything more complex than a plain type-based sort — like sorting an array of structs by one of their properties — pass a callback function instead. It receives two elements at a time and must return -1, 0, or 1 to say which comes first.
compare() is a built-in ColdFusion function for comparing two strings and returning -1, 0, or 1 — a natural fit for this callback.
Reversing an Array
First and Last Elements
Equivalent to fruits[1] and fruits[-1], just more explicit about intent.
Real-World Example: Ranking Scores
Common Beginner Mistakes
Treating arrayFind's 0 as false in a strict comparison
arrayFind returns a numeric index, or 0 when not found — 0 is falsy in an if check, but comparing arrayFind(arr, val) == false directly won't work as expected. Check for == 0 or just use it as a condition.
Forgetting arraySort is case-sensitive by default
"text" sort type sorts uppercase and lowercase separately (capital letters sort before lowercase in ASCII order) — use "textnocase" for a case-insensitive alphabetical sort.
Expecting arraySort with a callback to return a new array
Like the type-based version, the callback form also sorts in place and modifies the original array directly.
Best Practices
- Use the type-based arraySort for plain numbers or strings; use a callback only when sorting by something more specific, like a struct's property.
- Use arrayContains for a simple existence check — it reads more clearly than checking arrayFind(...) != 0.
- Use "textnocase" for user-facing alphabetical sorts, so "apple" and "Apple" sort together as expected.
Interview Questions
What does arrayFind return if the value isn't in the array?
0, not -1.
What's the difference between arrayFind and arrayFindAll?
arrayFind returns the index of the first match only. arrayFindAll returns an array of every matching index.
How do you sort an array of structs by a specific property?
Pass a callback function to arraySort that compares that property on the two elements it receives, returning -1, 0, or 1.
What does arraySort's callback function need to return?
-1 if the first element should come before the second, 1 if it should come after, and 0 if they're equal.
Summary
In this lesson, you learned how to search arrays with arrayFind, arrayFindAll, arrayContains, and arrayIndexExists, and how to sort them with arraySort — both by simple type and with a custom callback for more complex cases like sorting structs.
What's Next?
The next lesson covers functional array operations — arrayMap, arrayFilter, arrayReduce, and arrayEach, for transforming arrays without writing a manual loop.