merge multi-sorting of columns and rows into one method with a flag to specify rows versus column sorting