it looks you might be going about this in a convoluted way. If you can re-structure your data files this should be very easy. What you need are presorted files already on the server that you open depending on what the sort method is. The only time you need to sort is when a new entry is added. You put the new entry in all of the presorted files, then re-sort the files, then re-print them to disk already sorted by their respective sort criteria (age, name, whatever, whatever).

You presorted name file might look like this:

Andy,23,180 Bob,19,123 Fred,44,230 Joe,33,145

Your presorted age file might look like this:

Bob,19,123 Andy,23,180 Joe,33,145 Fred,44,230

Now this situation is not always possible, but if it is it will make your life much easier and your scripts run faster since the sorting may only need to be done once in a while (when a new entry is added) instead of everytime the files are opened. -------------------------------------------------

The following is based on code from the perlfunc docs which you should study. 'cmp' is for comparing strings. '<=>' is for comparing numbers. The logical OR operator let's us choose the appropriate comparison method. @newlines is a slice of @lines sorted according to what's stored in @particulars. If you want the lines upside down, swap the positions of $a and $b in the sort function. And remember, when you name functions after user input it can be dangerous, so untaint your input.