Using sortperm instead of sortrows for improved speed, memory use