magicsort function

Returns a sorted data matrix