Requests for crystal structure files to be exported with Cartesian values in order of precedence using Perl script

Dear Developers and Users, 

I would like to use Perl script to export a crystal structure file, using Cartesian coordinates, so that it can be sorted first according to the height of the Z-value, then according to the Y-value when the Z-value is the same, and finally according to the X-value, maintaining the overall atomic positions unchanged after the sorting, may I know how to export the file in which format or how to achieve this operation?