Polymorphic sort function implementation