Implemented sorting functionality