implementation of the sort methods. this is tentative so you are more than welcome to modify as needed