I have an array of structs in ColdFusion. I\'d like to sort this array based on one of the attributes in the structs. How can I achieve this? I\'ve found the StructSort fun
I wanted to throw my two cents in here. I ran into a case where I needed to sort an array of structures using more than one key. I wound up using a constructed query to do my sorting. The function takes the array of structs as the first argument, and then an array of structs indicating the sort order, like this:
Within the sortArrayOfStructsUsingQuery function, I construct a query based only on the keys I pass in, then sort that query. Then, I loop over the query, find the structure element from the array which matches the data at the current query row, and add that structure to the array I hand back.
It's entirely possible there's a gaping hole in this code that my testing hasn't uncovered (there haven't been a lot of use-cases for me yet), but in case it's useful to anybody, here it is. Hope it's useful, and if there are any glaring holes, I'm happy to hear about them.
(just a note: I use the "local" scope for all variables that will stay in the function, and the "r" scope for anything I intend to hand back, for whatever that's worth)
SELECT *
FROM [local].query
ORDER BY #local.order.clause#