Move sort shapes function out.