Fixed a flaw with sorting