Structs don't have a built-in order, so "sorting" a struct really means getting its keys back in a specific order — as an array, or as a newly built struct. This lesson closes out structs with sorting, key/value extraction, and searching recursively through nested structs.
Learning Objectives
After completing this lesson, you'll be able to:
- Get a struct's keys sorted with structSort.
- Pull all of a struct's keys or values into an array.
- Sort by a nested value using structSort's path option.
- Search recursively through nested structs with structFindKey and structFindValue.
structSort — Sorted Keys as an Array
structSort doesn't return a sorted struct — structs don't really have an order to preserve. It returns an array of the top-level keys, sorted the way you asked.
Use the sorted key array to loop through the struct in that order: for (key in sortedKeys) { ... scores[key] ... }
Sorting by a Nested Value
The optional path argument sorts by a key inside each top-level value, instead of the top-level key itself.
Sorted by each employee's department (Computer, General, Physical), not by the employee's own key name.
Keys and Values as Arrays
structKeyList(user) does the same as structKeyArray but returns a delimited string instead of an array.
Searching by Key: structFindKey
Recursively searches through a struct (or array) of nested structs for a specific key name, returning where each match was found.
Each result includes path (where it was found), owner (the containing struct), and value.
Searching by Value: structFindValue
Both structFindKey and structFindValue search recursively through nested structs — not just the top level.
Real-World Example: Top 3 Scores
Common Beginner Mistakes
Expecting structSort to return a sorted struct
It returns an array of sorted keys — you loop through that array and look each value up in the original struct, rather than getting a reordered struct directly.
Forgetting structFindKey and structFindValue search recursively
They search through nested structs at every level, not just the top — results can include matches several levels deep, which is exactly the point of using them over a plain structKeyExists check.
Using "one" scope and being surprised only one result comes back
The scope argument defaults to returning just the first match — pass "all" explicitly if you need every matching result.
Best Practices
- Use structSort's path argument to sort by a nested property instead of manually extracting values first.
- Prefer structKeyArray over structKeyList when you'll be looping or further processing the keys — arrays are more natural to work with than delimited strings.
- Reach for structFindKey / structFindValue specifically when the data is nested and you don't know exactly where a value lives — for flat, known structs, a direct key check is simpler.
Interview Questions
What does structSort actually return?
An array of the struct's top-level keys, sorted — not a reordered struct.
How do you sort a struct by a value nested inside each entry?
Pass the nested key's name as structSort's fourth argument (the path parameter).
What's the difference between structFindKey and structFindValue?
structFindKey searches for a specific key name; structFindValue searches for a specific value. Both search recursively through nested structs.
Summary
In this lesson, you learned to sort a struct's keys with structSort (including by a nested property), extract keys and values as arrays, and search recursively through nested structs with structFindKey and structFindValue.
This wraps up structs for this module — next comes Lists, ColdFusion's delimited-string data type.
What's Next?
The next lesson covers Lists — ColdFusion's delimited-string data type, and how any string can be treated as one.