seperate out an array sorting function

otherwise everything has to be in the edges and that can be a pain. I also refactored a little so arguments don't get mutated in the newly exposed function.