Skip to content

Instantly share code, notes, and snippets.

@kevindb
Created June 15, 2016 14:21
Show Gist options
  • Save kevindb/5ae3724c7a724b50bd4b3b404cbcd263 to your computer and use it in GitHub Desktop.
Save kevindb/5ae3724c7a724b50bd4b3b404cbcd263 to your computer and use it in GitHub Desktop.
Basic recursive sort in ColdFusion
component {
public array function sort (
required array arr,
numeric pos = 1
) {
local.nextPos = arguments.pos + 1;
if (arguments.arr[arguments.pos] > arguments.arr[local.nextPos]) {
local.tempLeft = arguments.arr[arguments.pos];
arguments.arr[arguments.pos] = arguments.arr[local.nextPos];
arguments.arr[local.nextPos] = local.tempLeft;
if (arguments.pos > 1) {
local.prevPos = arguments.pos-1;
} else {
local.prevPos = 1;
}
arguments.arr = sort(arr=arguments.arr, pos=local.prevPos);
} else if (local.nextPos < arrayLen(arguments.arr)) {
arguments.arr = sort(arr=arguments.arr, pos=local.nextPos);
}
return arguments.arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment