Fixed the sorting function.