Re-engineer the derived Ord instance generation code (fix Trac #4019)