sort: simplify the code to handle -z flag
(cherry picked from commit f079ef8aa428ebffad28f5c62cce52c2bf70fb49)