Sort an array with a user-defined comparison function and maintain index association