QuerySort with a list of values

I am attempting to add rows to a query and then re-sort the query. I am adding the rows with queryAddRow(), and would then like to use querySort() to re-sort.

My MySQL query has a sort operation as such (I am sorting in directional order of a compass rose, the column is 'Direction'):

ORDER BY FIELD(Direction, 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW')

I would like to replicate this functionality using querySort(), if it’s possible. I see hints that this may be possible in the Lucee.org documentation, but no examples or guidance. Does anybody have thoughts on how this could be done? Thank you in advance!

Try this, where data is your query object:

directions = [ "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" ].Each( ( direction, index ) => directionsOrdered[ direction ] = index );
data.Sort( ( row1, row2 ) => return ( directionsOrdered[ row1.direction ] - directionsOrdered[ row2.direction ] ) );
1 Like

This was highly effective. Thank you for this solution, hopefully it can be preserved for others looking for this help in the future!