Fully implemented sorting