Sort the elements in the given matrix having one or more dimension