Minor changes to sort matrix

Split sort matrix subroutines into seperate files and made some changes
to make them more efficient.