After a few rants and rumblings, it’s time for something CF related. Once again I want to talk about a function that is not widely known/used: structSort. structSort allows you to take a collection of structs and sort them by a key.

So, imagine you have a struct of structs:

kids = structNew();
for(x=1; x lte 5; x=x+1) {
   kids[“Child #x#”] = structNew();
   kids[“Child #x#”] .age = randRange(1,5);
   kids[“Child #x#”] .name = “Child #x#”;
   kids[“Child #x#”].complex = structNew();
   kids[“Child #x#”].complex.x = randRange(1,10);

structSort will allow you to sort “kids” by age, name, or even complex substructs, in this case, the complex.x key. The basic usage of structSort is:

structSort(struct, type of sort, direction of sort, path to sort);

So, to sort my above data by age, I can use:

orderedKids = structSort(kids,”numeric”,”asc”,”age”);

structSort returns an array of keys, where the key represents one of the children in my kids struct. I can then loop over my array to display my struct of structs in an ordered fashion. To search the more complex aspect of the struct, I just use the proper path:

complexOrder = structSort(kids,”numeric”,”asc”,”complex.x”);

One thing to watch out for - when you type in “structSort(“ in DWMX2004, it incorrectly tells you that the 2nd argument is the path. (Which would actually make more sense.) HomeSite+ does not have this issue.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA